Warning: Unsuccessful read: Matching failure in format.. Subscripted assignment dimension mismatch.
4 views (last 30 days)
Show older comments
clear all; clc;delete(instrfind);
% on the ethernet shield, from the sensor Vcc (red) goes to 5v on the
% analog side. GRND (black) goes to ground on analog side. DAT (orange)
% pin2 on TX/RX side.
%creates serial object and manually adjusts fields
a = serial('com8');
set(a,'BaudRate',9600);
set(a,'Timeout',65);
set(a,'Terminator','LF')
%connects arduino to MATLAB- I guess opens port
fopen(a);
x = 1:60; %readings over the span of one hour
for i=1:length(x)
y(i) = fscanf(a, '%f' ,6);
z(i) = fscanf(a, '%f',6);
end
fclose(a);
delete(a)
clear a
0 Comments
Answers (1)
Walter Roberson
on 11 Apr 2016
You would get that error if the other end is sending something that cannot be interpreted as a number or as whitespace, such as if it is sending text or if it is sending the numbers in binary.
2 Comments
Walter Roberson
on 11 Apr 2016
When fscanf() encounters something that does not match the format, it stops reading at that point and "puts back" whatever characters it encountered that it could not deal with, and then it returns whatever data it was able to process. It might, for example, return 4 values when the format implies you want 6. If you are not expecting that possibility and are storing into an array then you could end up with subscript dimension mismatches.
N = 6;
yvals = fscanf(a, '%f' ,N);
if length(yvals) < N
fprintf('expected %d values, only received %d, padding to %d\n', N, length(yvals), N);
yvals(N) = 0; %pad out
end
yvL = length(yvals);
yiL = length(y(i));
if yvL > yiL
fprintf('We have %d values but only %d locations to write into. Extra values will be discarded\n', yvL, yiL);
y(i) = yvals(1:yiL);
elseif yvL < yiL
fprintf('We have only %d values, but %d locations to write into. Padding to length %d\n', yvL, yiL, yiL);
y(i(1:yvL)) = yvals;
y(i(yvL+1:end)) = 0;
else
y(i) = yvals;
end
and likewise for z
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!