Serial communication with Arduino error "Index exceeds matrix dimensions"

3 views (last 30 days)
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.

Answers (1)

Sanket Mishra
Sanket Mishra on 11 Jul 2014
Edited: Sanket Mishra on 11 Jul 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 Comments
Dragomir
Dragomir on 11 Jul 2014
Edited: Dragomir on 11 Jul 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 on 13 May 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.

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!