フィルターのクリア

How can I extract the values ​​from Vector Double? Index exceeds the number of array elements (0). Error in test1 (line 19) ax=m(1)

1 回表示 (過去 30 日間)
I got the error message, can you please help me?
Index exceeds the number of array elements (0).
Error in test1 (line 19)
ax=m(1)
Code :
delete(instrfindall);
clear all
clc
serialPort = 'COM2';
s=serial(serialPort,'BaudRate',9600);
fopen(s);
%tic
m=[]
while 1
x=fscanf(s) ;
m=[ x];
m=str2num(m)
disp(m);
ax=m(1)
ay=m(2)
az=m(3)
end
fclose(s);
Outbut -------->
m =
[]
m =
-0.0400 0.2700 0.3900 42.8000 15.0000 -5.0000 -7.0100 1.2200
-0.0400 0.2700 0.3900 42.8000 15.0000 -5.0000 -7.0100 1.2200
ax =
-0.0400
ay =
0.2700
az =
0.3900
m =
[]
Index exceeds the number of array elements (0).
Error in test1 (line 19)
ax=m(1)

採用された回答

Walter Roberson
Walter Roberson 2020 年 7 月 16 日
you are in a loop reading from the serial port. At some point it sends you an empty line. You should check isempty(m) before you index into m
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 7 月 17 日
delete(instrfindall);
clear all
clc
serialPort = 'COM2';
s=serial(serialPort,'BaudRate',9600);
fopen(s);
%tic
m=[];
while 1
x = fscanf(s);
m = str2num(x);
if isempty(m); continue; end
ax = m(1)
ay = m(2)
az = m(3)
end
fclose(s);
Yamen Madamani
Yamen Madamani 2020 年 7 月 17 日
Walter Roberson worked, thank you very much, you saved me

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSerial and USB Communication についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by