MATLAB: Problem of ??? In an assignment A(I) = B, the number of elements in B and I must be the same.

str2num

I got this error.
*??? In an assignment A(I) = B, the
number of elements in B and
I must be the same.
Error in ==> readserial at 18
time(k)=str2num(out(13:17));*
Here is my script:
s = serial('COM2');
set(s, 'baudrate', 38400)
fopen(s)
out = fscanf(s)
%out='DATA,TIME,#,10006,1.49,1.50,0,0';
totaldata=500;
time=zeros(1,totaldata);
emg1=zeros(1,totaldata);
emg2=zeros(1,totaldata);
fsr1=zeros(1,totaldata);
fsr2=zeros(1,totaldata);
for k=1:totaldata
out = fscanf(s)
if out(11)=='#'
time(k)=str2num(out(13:17));
emg1(k)=str2num(out(19:22));
emg2(k)=str2num(out(24:27));
fsr1(k)=str2num(out(29:29));
fsr2(k)=str2num(out(31:31));
end
end
fclose(s)
My intention is to collect data for 500 samples and sort it out to time, emg1, emg2, fsr1 and fsr2 throughout the process.
Thanks.

Best Answer

display "out" as you go. I suspect you will find that at some point there will be a number with fewer than 5 digits at position 13, leading to a situation in which positions 13:17 appear to contain two numbers separated by comma. str2num() of such a string would return both numbers as part of a vector. str2num() eval()'s the string it is given rather than just parsing it as a number. For safety it is better to use str2double()