1
votes

How to display what's shown inside an Arduino's Serial Monitor , in a MATLab program? From Arduino we can display the output via serial monitor using serial.println and serial.print. But how to display the serial.println and serial.print from arduino inside MATlab? If in Qt programming, anything can be shown in QDebug, but I really don't know how it goes for MATLab.

Maybe just a simple serial.println('1') to show the character '1' in the serial monitor. Then, how can we program the MATLAB to read/detect the '1' and do something about it, such as.... creating a simple textfile?

Can anyone please teach me?

Thanks.

1

1 Answers

0
votes

First of all we are going to close the eventually open port:

%% Port reset:
delete(instrfindall);

Then you have to open a communication with your arduino (check your arduino IDE to know which port is selected). Also select the right BaudRate.

%% Serial open
arduino=serial('COM4','BaudRate',9600);

Then you can start to read the arduino output. Let's say that we want to read the 10 first outputs. (so the 10 first serial.printxx)

%% Start to read arduino's values and write the result in results.txt
fopen(arduino)
fid = fopen('results.txt','wt');
for i=1:10
    y = fscanf(arduino,'%s');
    fprintf(fid,'my arduino output is %s\n',y)
end
fclose(fid);
fclose(arduino);

Matlab will read the arduino value as a string, I let you cast this string into whatever you need.

IMPORTANT:

You need to install the arduino support package for matlab

ADVICE

I usually read the output using an endless while loop that check on every step if the loop need to be skipped.

For example if your arduino code send a Serial.println('exit')

my matlab code will look like:

while 1
    y = fscanf(arduino,'%s');
    if strcmp(y,'exit')
       break
    end 
    fprintf('my arduino output is %s\n',y)
end