6 years, 9 months ago.

Sending array from mbed to Matlab

Hi

I'm new to serial interfacing and am having some issues sending an array from mbed to matlab to do further manipulations with so would appreciate some advice, I have the serial set up correctly as I have no issue printing the data to terminal, I'm just unable to read it in MATLAB. the key bit of mbed code is as follows:

    int trigger = 1; //matlab trigger const
    for(int i =0; i<NumReadings;i++){
        pc.printf("%d", trigger); //print trigger to matlab so it knows next info is data
        pc.printf("%d,%d,%d,%f\n\r",lftArr[i],rgtArr[i],linArr[i],timArr[i]); //print values
        wait(0.03f);
    }
    trigger = 2; //trigger value to tell matlab data package has finished
    pc.printf("%d\n\r", trigger);

And respectively I have the following MATLAB code:

%fclose(instrfind) 
s1 = serial('/dev/tty.usbmodem1423'); %Update address to where mbed is
fopen(s1); %Open port
trig_read = 1; %Two variables send from mbed as checks for data
trig_end = 2;
NumReadings =100;
readings = zeros(100,4);
while(1)
    trigger=fgetl(s1);
    trigger=sscanf(trigger,'%d');
    if trigger==trig_read 
        tmp = fgetl(s1);
        tmp = sscanf(tmp,'%d,%d,%d,%f');
        readings(counter+1,1:4) = tmp; %Column 1      
        trigger=0;
    end
    if trigger == trig_end
        break;
    end
end

I've tried to send trigger bytes so matlab knows when data is incoming but it does not seem to be able to read it at all.

Any advice would be greatly appreciated,

Cheers!

Note, I should probably clarify that I am using MATLAB R2016b.

Be the first to answer this question.