How can i scan data and store it in an array?

조회 수: 3 (최근 30일)
Hardika Jain
Hardika Jain 2015년 2월 3일
답변: Rajanya 2025년 1월 28일
code: clear all clc arduino=serial('COM19','BAUDRATE',9600);
fopen(arduino);
y = zeros(1,10);
for i=1:10
y(i) = fscanf(arduino,'%d');
end
disp(y);
fclose(arduino);
the error I am getting is: In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in plotpl (line 7) y(i) = fscanf(arduino,'%d');
so how can I store the scanned value in an array?

답변 (1개)

Rajanya
Rajanya 2025년 1월 28일
The reason for this error is mostly because the left and right side of the below assignment has different number of elements.
y(i) = fscanf(arduino,'%d');
If the scanned value is an array containing 10 elements as I assume, instead of using a loop that accesses only a single index of 'y' and attempts to assign the entire array to that single index, you can declare 'y' as an array of 10 elements and directly assign the scanned array to y.
For example, refer to the demo example given below:
Here also, we get the same error since we are trying to assign a vector 'v' to a single element. Taking care of the types and sizes of the left hand and right hand operands during an assignment will avoid all such errors.
Thanks, hope this helps!

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by