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