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
댓글 수: 0
답변 (1개)
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
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 Center 및 File 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!