Serial communication with Arduino error "Index exceeds matrix dimensions"

3 vues (au cours des 30 derniers jours)
Dragomir
Dragomir le 11 Juil 2014
Commenté : harold le 13 Mai 2018
I am trying to receive from Arduino, 4 sensor data that look like this:
"v4 388 v7 576 v8 319 v5 622
v4 388 v7 563 v8 327 v5 622
v4 368 v7 447 v8 231 v5 558"
v4....v8 are sensor names and the numbers are their value.
This is the code I am using:
% clear all
clc
arduino=serial('COM15','BaudRate',9600);
fopen(arduino);
x=linspace(1,100);
numcols = 1;
y = {};
for i=1:length(x);
data =fscanf(arduino,'%f');
IncomingString = char(data);
IncomingString = regexp(IncomingString, '\s*', 'split');
v4(i,1)= IncomingString(1,2);
v7(i,1)= IncomingString(1,4);
v8(i,1)= IncomingString(1,6);
v5(i,1)= IncomingString(1,8);
end
fclose(arduino);
PlotV4 = str2double(v4);
figure(1)
plot(PlotV4);
title('V4'); xlabel('Samples'); ylabel('Volt');
PlotV7 = str2double(v7);
figure(2)
plot(PlotV7);
title('V7'); xlabel('Samples'); ylabel('Volt');
PlotV8 = str2double(v8);
figure(3)
plot(PlotV8);
title('V8'); xlabel('Samples'); ylabel('Volt');
PlotV5 = str2double(v5);
figure(4)
plot(PlotV5);
title('V5'); xlabel('Samples'); ylabel('Volt');
%Written by Kasper Stentz
At line with " v4(i,1)= IncomingString(1,2); " I get the error "Warning: Unsuccessful read: Matching failure in format. Index exceeds matrix dimensions."
Can someone please help me? I need to extract at least 100 values each and plot them.

Réponses (1)

Sanket Mishra
Sanket Mishra le 11 Juil 2014
Modifié(e) : Sanket Mishra le 11 Juil 2014
In your FOR iteration when i exceeds 3 then the code points to an index say v4(4,1) corresponding to which there does not exist any value (from data you provided). Try using below code
x= linespace(1,3);
for i=1:length(x);
I hope above helps.
  2 commentaires
Dragomir
Dragomir le 11 Juil 2014
Modifié(e) : Dragomir le 11 Juil 2014
Same error. I am sorry I didn't mentioned I need al least 100 data points. It will have to read at least 100 rows from the serial.
harold
harold le 13 Mai 2018
hey, how is it going? I face a similar problem. Have you solved yours? If so, would you mind sharing the code? Thx in advance.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by