Matlab Sun Position

I'm trying to send my Arduino sun position angular coordinates over the serial port, this is for a sun tracking skylight concept i have been stewing over for a long time now. Ideally i want to send floating point values so i did a bit of searching and found some stuff on bit shifting the binary serial data and recombining to give the final large value on the arduino. Only thing is I cant get it to work with matlabs fwrite over serial.
here's my matlab code

clear all
clc
s = serial('COM4');
set(s,'BaudRate',9600);
fopen(s);

a = uint16(1230); %random test number a
       disp(sprintf('Initial uint16 value %5d is %16s in binary', ...
          a,dec2bin(a)))
 %out = fscanf(s)     
%Find Highbyte by bitshifting
highbyte = bitshift(a,-8);
highbyte = uint8(highbyte);
    disp(sprintf('Shifted uint8 highbyte value %5d is %16s in binary',highbyte,dec2bin(highbyte)))
 


% Find lowbyte by moving highbyte up 2^8 and subtracting from a
b= uint16(highbyte)*(2^8);
lowbyte = a-b;
lowbyte = uint8(lowbyte);
    disp(sprintf('Shifted uint8 lowbyte value %5d is %16s in binary',lowbyte,dec2bin(lowbyte)))

%Write over serial
fwrite(s,highbyte)
fwrite(s,lowbyte)

%Read returned info
Highbyte = fscanf(s)   
Lowbyte = fscanf(s)
angle = fscanf(s)  

fclose(s)
delete(s)
clear s

and here's my Arduino code

int angle = 0;
int oldangle = 0;
int highbyte = 0;
int lowbyte = 0;      // for incoming serial data

void setup() {
      Serial.begin(9600);      // opens serial port, sets data rate to 9600 bps
}

void loop() {

      // send data only when you receive data:
      if (Serial.available() > 1) {
   
            // read the incoming byte:
            highbyte = Serial.read();

        if (Serial.available() > 0) {
                lowbyte = Serial.read();
        }

              angle = (highbyte << 8) + lowbyte;
                delay(1);
        if (angle != oldangle) {
                Serial.print("I received highbyte: ");
                Serial.println(highbyte, BIN);
                
                Serial.print("I received lowbyte: ");
                Serial.println(lowbyte, BIN); 
                
            Serial.print("I received: ");
            Serial.println(angle, BIN);
        }
oldangle = angle;
}
}

Now im not sure if my matlab code is 100% correct, like do i even need to split the value on the matlab side and send it seperatly? Or will the serial buffer accept a few bytes of data at once? also is fwrite even the right command in this case? All the examples iv found use fprintf but it seems this is only for strings.. (which i understand is only useful for text)

anyway if i type say 88 into the arduino serial monitor i get values returned which make sense. ie.

I received highbyte: 111000

I received lowbyte: 111000

I received: 11100000111000

But again this test is sending the character 8 twice not the number 88.

If anyone has any experience with this kind of thing any pointers would be much appreciated. This has turned into a rather annoying roadblock for me and this project will be totally awesome once i have pulled a few of these details together.

Instead of calculculating the Sun's position, have you considered sensing the position?

For example...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1224541956

I know this isn't really the simplest way to go about it but its more of an engineering exercise for me, and I like the idea of a smarter type of control. I am using PID on dc motors with rotary encoders which is all part of the exercise. I may incorporate some sensors later on to add some robustness. Ideally the sun position calculation would be slimmed down and run on the arduino with some compensation added by light sensors. But right now I want to get the motor control etc working and actually track the sun.

Actually just realized this thread is in the hardware interfacing section my bad.

I can provide some help...

do i even need to split the value on the matlab side and send it seperatly?

According to the MATLAB documentation (MATLAB Documentation), you will not need to split the values. Floating-point values on the Arduino are IEEE compatible so "single" or "double" for the precision parameter with a floating-point number for the A parameter should work. I have no idea what the endian is for the Arduino but machineformat will either be 'ieee-be' or 'ieee-le'.

Or will the serial buffer accept a few bytes of data at once?

The default serial buffer on the Arduino is 128 bytes.

is fwrite even the right command in this case?

Certainly looks like it to me.

You're not the first person to have problems with MATLAB's serial communication...

http://www.google.com/search?q=matlab+serial+site%3Aarduino.cc
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1255532486
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1173473394

...unfortunately, there hasn't been a great deal of follow-up.

Thanks Coding Badly. turns out there is about a one second delay once the serial comms is opened before the arduino buffers any serial data. apparently this is because arduino needs that time to look and see if it gets data on it's serial port to indicate that it is not to run it's program, but to run it's bootloader.
As in this post:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1245164326

Anyway this seems like an issue that would have cropped up a lot before when interfacing with other software, not just with matlab.