
How can i scan data and store it in an array?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponses (1)
Rajanya
le 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.
You can also have a look at a similar question here - https://www.mathworks.com/matlabcentral/answers/158915-in-an-assignment-a-i-b-the-number-of-elements-in-b-and-i-must-be-the-same
Thanks, hope this helps!
0 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!