How to stop the program if an array is empty?

4 Ansichten (letzte 30 Tage)
Mariana
Mariana am 11 Feb. 2020
Kommentiert: Mariana am 12 Feb. 2020
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

Akzeptierte Antwort

Ajay Kumar
Ajay Kumar am 11 Feb. 2020
Bearbeitet: Ajay Kumar am 11 Feb. 2020
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 Kommentar
Mariana
Mariana am 12 Feb. 2020
I am doing it with in Simulink and I have an error message when using try and catch on my simulink implementation.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by