フィルターのクリア

"index exceeds matrix dimensions" when using str2double

1 回表示 (過去 30 日間)
Heejin Kim
Heejin Kim 2017 年 7 月 27 日
コメント済み: Heejin Kim 2017 年 7 月 27 日
Hi guys.
I am currently attempting to receive data from Arduino and saving them into a Matlab array. I'm not sure why, but I got stuck in an error "index exceeds matrix dimensions" while trying to save str2double value into an array location. I'm wondering if this is because I'm not allowed to save any double values into an array? How should I come across this :( Below I've attached my code.
Thanks in advance.
//////////////////////////////////////
clc
clear s
delete(instrfindall);
%initializing port connection
s=serial('COM7','BaudRate',9600,'DataBits',8);
s.StopBits=1;
fopen(s);
%receiving text info from COM7
n=10;
array=zeros(n,3);
for i=1:n
fprintf(s,'*IDN?');
answer = fscanf(s)
angle=textscan(answer,'%c %s %s %s %c');
array(i,1)=str2double(angle{i,3});
array(i,2)=str2double(angle{i,4});
array(i,3)=str2double(angle{i,5});
end
%converting string info to double and saving them into array
fclose(s);
delete(s)
clear s
  5 件のコメント
Heejin Kim
Heejin Kim 2017 年 7 月 27 日
@KSSV Sorry I am not quite sure what you mean by "before and after", but the angle info from arduino is a 1×5 vector, from which I needed the 3rd, 4th, and 5th string value.
Jan
Jan 2017 年 7 月 27 日
@Jaz: See my answer: Then you need "angle{3}" and not "angle{i,3}".

サインインしてコメントする。

採用された回答

Jan
Jan 2017 年 7 月 27 日
編集済み: Jan 2017 年 7 月 27 日
Text scan replies a cell vector as output. Therefore the first index "angle{*i*, 3}" is misplaced:
for i=1:n
fprintf(s,'*IDN?');
answer = fscanf(s)
angle = textscan(answer,'%c %s %s %s %c');
array(i,1) = str2double(angle{3}); % instead of "angle{i,3}"
The output of textscan will not depend on the loop counter i.
I'm wondering if this is because I'm not allowed to save any double
values into an array?
On one hand it is wise to use phantasy to examine different possible sources of problems. On the other hand, speculations are less useful as the debugger. Type this in the command window:
dbstop if error
or use the corresponding menu of the editor. Then run the code until it stops in the failing line. Now check the details in the command window:
% Failing line: array(i,1)=str2double(angle{i,3});
size(angle)
class(angle)
i
angle{i,3}
str2double(angle{i,3})
which('str2double') % function shadowed by a variable?
Using the debugger is more efficient than asking the forum.
  1 件のコメント
Heejin Kim
Heejin Kim 2017 年 7 月 27 日
Thanks for the solution & advice!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by