How to stop the program if an array is empty?

조회 수: 4 (최근 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 CenterFile Exchange에서 Signal Attributes and Indexing에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by