Serial communication with Arduino error "Index exceeds matrix dimensions"

조회 수: 3 (최근 30일)
Dragomir
Dragomir 2014년 7월 11일
댓글: harold 2018년 5월 13일
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에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by