Warning: Unsuccessful read: Matching failure in format.. Subscripted assignment dimension mismatch.

4 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2016-4-11
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 个评论
Nicholas  Jacobs
Nicholas Jacobs 2016-4-11
Thanks for your very fast reply. I thought that regardless of what the Arduino is sending that fscanf would convert it. Looking at the documentation I see that there is an option to display a msg. the Doc says that if there isn't an output for a message I'll get a warning in the CL. Given I'm using in a for loop I guess I can't see it.
Walter Roberson
Walter Roberson 2016-4-11
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by