Sending serial data from Arduino to Matlab error
4 views (last 30 days)
Show older comments
I need to use arduino as a diy "slider" to send serial data to matlab over the span of a few minutes. There is the chance of receiving an error message during that time, not allowing me to further use the arduino.
Error code as follows:
Warning: Escaped character '\h' is not valid. See 'doc sprintf' for supported special characters.
> In matlabshared.asyncio.internal/MessageHandler/onError (line 31)
In readManVAS (line 29)
In Exp_final (line 309)
Error using matlabshared.asyncio.internal.MessageHandler/onError (line 31)
Das Gerät erkennt den Befehl nicht [system:22 at \mathworks
Error in readManVAS (line 29)
pause(0.02);
^^^^^^^^^^^
Error in Exp_final (line 309)
rating = str2num(readManVAS(s1))/10.23;
^^^^^^^^^^^^^^
(The Exp_final script at line 309 is simply using readManVAS)
Matlab code:
function VAS=readManVAS(s)
% read serial input line by line until input starts with a space AND
% can be converted into a scalar number. If 20 readlines fail, return a
% nan value. finally flush the serial buffer once to clean up.
firstChar="1";
maxRead=20;
nRead = 0;
while firstChar~=" "
nRead = nRead+1;
if nRead>maxRead
VAS=NaN;
flush(s);
return;
end
try
tmp=char(readline(s));
firstChar=tmp(1);
if isnan(str2double(tmp))
firstChar='1';
end
catch
firstChar="1";
end
pause(0.02);
end
flush(s);
VAS=tmp;
%VAS=num2str(str2double(tmp));
end
Arduino Code:
// Arduino sketch for B10K Potentiometer
# include "RunningMedian.h"
const int POTPIN = A0;
RunningMedian samples = RunningMedian(23);
int potValue;
char outStr[5];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//for (int i=0;i<sizeof(potValues);i++){
// potValues[i] = 0;
//}
}
void loop() {
// put your main code here, to run repeatedly:
potValue = analogRead(POTPIN);
samples.add(potValue);
potValue=samples.getMedian();
sprintf(outStr," %d",potValue);
Serial.println(outStr);
delay(10);
}
I'm thinking that non-numerical strings may be the issue, but I don't see them as is on the serial monitor.
If important, I am using Psychtoolbox 3.0.19.
I'm grateful for any kind of suggestions and steps to troubleshoot this.
Thanks
4 Comments
Walter Roberson
on 15 Jul 2025
Warning: Escaped character '\h' is not valid. See 'doc sprintf' for supported special characters.
> In matlabshared.asyncio.internal/MessageHandler/onError (line 31)
In readManVAS (line 29)
If I count correctly, line 29 of readManVAS is the pause() statement. It does not make any sense for the pause() statement to be generating that error message....
Unless there is asynchronous I/O happening in the background, such as accumulating data from the serial port into a buffer, and somehow the asynchronous I/O is generating that error. It does make sense for asynchronous I/O to be happening. It does not make sense for asynchronous I/O to be generating that particular error message. Any asynchronous I/O should just be buffering the incoming data, or at most doing end-of-line detection on the incoming data, neither of which would involve sprintf() ... unless there is an especially poor implementation.
I also note that the arduino code you show cannot emit a \h sequence, so I really don't understand what is happening.
Answers (1)
Aishwarya
on 9 Jul 2025
After reviewing the "readManVAS" function, I believe it can be optimzed better. Consider the following changes in the function:
- There is no requirement for extra "firstChar" variable checking. You can check if the first char is whitespace using "startsWith(tmp, ' ')" function.
- Put while loop condition so that it calls "readline" function 20 times and when space is encountered return the "VAS" value.
- Given that you are expecting the VAS value to be numeric value, you can use "isstrprop" function to check if the characters are of "digits" category.
- Before the "tmp" value is saved to "VAS" variable, you can remove the trailing and leading whitespaces using "strtrim" function.
Here is the modified "readManVAS" function:
function VAS=readManVAS(s)
maxRead = 20;
nRead = 0;
% Assign VAS as NAN value default
VAS = NaN;
% Call readline 20 times
while nRead < maxRead
nRead = nRead + 1;
try
tmp = char(readline(s));
% Check if tmp starts with ' ' and is not empty and is all
% numeric avlue
if startsWith(tmp, ' ') && ~isempty(tmp) && all(isstrprop(strtrim(tmp), 'digit'))
% Save tmp after removing trailing spaces
VAS = strtrim(tmp);
flush(s);
return;
end
catch ME
% To log when error is captured
warning("Serial read error: %s", ME.message);
end
pause(0.02);
end
flush(s);
end
Feel free to change this function according to your requirement.
In Arduino code, "Serial.println(outStr)" might not cause issue but sometimes buffer overflow or noisy characters can sneak in. Consider limiting the "outStr" value to certain digits as shown below:
// Limit to 5 digits + leading space
sprintf(outStr, " %05d", potValue);
Serial.println(outStr);
delay(10);
Kindly refer to the following documentation regarding functions used in the code:
- "sartsWith" function: https://www.mathworks.com/help/matlab/ref/string.startswith.html
- "isstrprop" function: https://www.mathworks.com/help/matlab/ref/isstrprop.html
- "strtrim" function: https://www.mathworks.com/help/matlab/ref/strtrim.html
I hope these changes help resolve the error.
See Also
Categories
Find more on MATLAB Support Package for 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!