unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

2 次查看(过去 30 天)
if ~isempty(instrfind)
fclose(instrfind)
end
arduino=serial('COM3','BaudRate',9600);
fopen(arduino);
i=1;
tic;
while (toc<=400)
%while 1
time(i) = toc;
s(i) = fscanf(arduino,'%c');
% subplot(3,1,1);plot(time,R,'linewidth'1.5);
% xlabel('t');ylabel('v');title('Reference');grid ON;
%
% subplot(3,1,2)
plot(time,s,'linewidth',1.5);
xlabel('time');ylabel('v');title('test');grid ON;
%
% sub[lo(3,1,3);plot(time,R,time,s,'linewidth',1.5);
% xlabel('t'):ylabel('v');title('Together');legend('Ref','Test');gride ON;
drawnow
i=i+1;
end

回答(1 个)

Guillaume
Guillaume 2020-2-25
The documentation is not very clear, but I believe that fscanf(arduino, '%c') is the same as fscanf(arduino) and that this reads as many characters as are available, not just one. So, if you want to read just one character at a time, then:
s(i) = fscanf(arduino, '%c', 1); %read just one character
If you meant to read all characters then you can't store multiple characters as a single element i of a vector, so you'll have to read the text as element of a cell array or a string array:
s{i} = fscanf(arduino, '%c');
%or
s(i) = string(fscanf(arduino, '%c'));

类别

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