How to stop the program if an array is empty?

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 String 的更多信息

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by