How can I read data from the arduino serial monitor using Matlab?
4 views (last 30 days)
Show older comments
I am trying to read the outputs of my Arduino serial monitor to graph them in Matlab. The serial monitor reads the data correctly, they are force values from 4 load cells. However, when using fscanf to read the serial monitor using Matlab, the data comes across as 0.0000 or -0.1200, which is what the load cells display during no load. How can I have Matlab read the actual values from the serial monitor rather than always getting these incorrect values? My code is the following:
handles.UltraM = serial('COM6','BaudRate',9600,'Timeout',10000000);
fopen(handles.UltraM);
%the code below is in a while loop with the variable "some_number" moving actuators to change the force exerted on the load cells
fprintf(handles.UltraM,'%s\n',(['<P' some_number '>'])); %input into the serial monitor to move the actuators, which works
data = fscanf(handles.UltraM);
[s1 s2] = strread(data, '%f %f','delimiter',', ');
s1_data = s1;
s2_data = s2;
The Arduino code that inputs a string into the serial monitor is:
Serial.println(String(sumForce) + ", " + String(int(yPos)));
The resultant codes display 2 numbers on the serial monitor. The first is a force reading (95.0000) and the second is a position (1300). They are displayed being separated by a comma and a space. In Matlab, the variable named "data" displays 0.0000, 0 or -0.1200, 600 which are the values that the serial monitor displays only when no force is exerted.
2 Comments
Answers (0)
See Also
Categories
Find more on Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!