How to stop the program if an array is empty?

3 ビュー (過去 30 日間)
Mariana
Mariana 2020 年 2 月 11 日
コメント済み: Mariana 2020 年 2 月 12 日
signal1 = [1,2,3,4,5,6];
signal2 = [7,8,9,10,11,12];
signal3 = [13,14,15,16,17,18];
signal4 = [19,20,21,22,23,24];
signal5 = [25,26,27,28,29,30];
signal6 = [31,32,33,34,35,36];
bufferSize = 6;
buffer = nan(bufferSize,6);
init = 1;
for i = 1:bufferSize
empty = sum(isnan(buffer(i,:))); %check status of each row
if empty == 6
buffer(i,1) = signal1(init);
buffer(i,2) = signal2(init);
buffer(i,3) = signal3(init);
buffer(i,4) = signal4(init);
buffer(i,5) = signal5(init);
buffer(i,6) = signal6(init);
init = init + 1; %infite number of data as input
if sum(isnan(signal1(1,init))) > 0
msgbox('Insufficient data')
break;
end
end
end
I have 6 signals of same size and I take the first value of each one of them and save them in the fist row of my buffer. However, when I change the bufferSize to 7 I have the following error:
Index exceeds matrix dimensions:
Error in test_array (line 28)
buffer(i,1) = signal1(init);
Its because the signal doesnt have enough data to fill the buffer.
I thought that by checking if the signal is expecting a new value before loading would stop the program before the warning, but it didnt worked.
if sum(isnan(signal1(1,init))) > 0
msgbox('Insufficient data')
break;
end

採用された回答

Ajay Kumar
Ajay Kumar 2020 年 2 月 11 日
編集済み: Ajay Kumar 2020 年 2 月 11 日
Handle the exception using try catch blocks
signal1 = [1,2,3,4,5,6];
signal2 = [7,8,9,10,11,12];
signal3 = [13,14,15,16,17,18];
signal4 = [19,20,21,22,23,24];
signal5 = [25,26,27,28,29,30];
signal6 = [31,32,33,34,35,36];
bufferSize = 6;
buffer = nan(bufferSize,6);
init = 1;
for i = 1:bufferSize
empty = sum(isnan(buffer(i,:))); %check status of each row
if empty == 6
buffer(i,1) = signal1(init);
buffer(i,2) = signal2(init);
buffer(i,3) = signal3(init);
buffer(i,4) = signal4(init);
buffer(i,5) = signal5(init);
buffer(i,6) = signal6(init);
init = init + 1; %infite number of data as input
try
signal1(1,init);
catch
msgbox(['Insufficient data at position ',num2str(init)])
break;
end
end
end
  1 件のコメント
Mariana
Mariana 2020 年 2 月 12 日
I am doing it with in Simulink and I have an error message when using try and catch on my simulink implementation.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by