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

1 view (last 30 days)
Hardika Jain
Hardika Jain on 3 Feb 2015
Answered: Rajanya on 28 Jan 2025
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?

Answers (1)

Rajanya
Rajanya on 28 Jan 2025
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!

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!