Problem of ??? In an assignment A(I) = B, the number of elements in B and I must be the same.
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Walter Roberson
on 15 Aug 2013
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()
3 Comments
Matt Kindig
on 15 Aug 2013
One way would be to use regexp() to split the string into parts, and then assign the parts appropriately. For example:
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)=='#'
params= regexp(out, '\,', 'split');
time(k)=str2double(params{4});
emg1(k)=str2double(params{5});
emg2(k)=str2double(params{6});
fsr1(k)=str2double(params{7});
fsr2(k)=str2double(params{8});
end
end
fclose(s)
More Answers (0)
See Also
Categories
Find more on String 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!