Serial communication with Arduino error "Index exceeds matrix dimensions"

2 次查看(过去 30 天)
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.

回答(1 个)

Sanket Mishra
Sanket Mishra 2014-7-11
编辑:Sanket Mishra 2014-7-11
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 个评论
Dragomir
Dragomir 2014-7-11
编辑:Dragomir 2014-7-11
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 2018-5-13
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by