Matlab having problem interpreting streamed string into numbers.
Info
This question is closed. Reopen it to edit or answer.
Show older comments
Hello , i am sending 0x61 over and over which is the letter 'a' to matlab as shown bellow.
But when i want to convert the recieved letters into number and plot it gives me all NAN instead of 61 as shown bellow.
sObject=serial('COM4','BaudRate',115200,'TimeOut',10,'Terminator','LF')
Why do i get this NAN,i tried to plot and i get blank screen,why it cant convert my sent data into numbers?
Thanks.
function callbackSerial(ser,~)
global time;
val=fscanf(ser);
numval=str2double(val)
%numval=dec2bin(val);
time(16)=numval;
time(1)=[];
disp(time);
figure(1),plot(time);
end


5 Comments
Rik
on 29 Oct 2020
What is the output you're getting from fscanf?
fima v
on 29 Oct 2020
'char like behavior'
I was asking you about data type and size. If you already have a numeric value, you don't need str2double.
data=hex2dec('61');
disp(char(data))%cast to char only for display
disp(str2double(data))
If you try to convert text to a number, but that text is not a number, you will get NaN.
fima v
on 29 Oct 2020
Stephen23
on 29 Oct 2020
See also related question:
Answers (1)
Steven Lord
on 29 Oct 2020
Please remove the semicolon from the end of this line, let your callback trigger a few times, and show us exactly what gets displayed in the Command Window.
val=fscanf(ser);
That information will likely help us determine how to "interpret" the data you're receiving from the serial port.
I suspect what you want is hex2dec not str2double, but the information about what exactly val is will support or refute that suspicion.
1 Comment
fima v
on 30 Oct 2020
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!