필터 지우기
필터 지우기

How to skip error and continue (NOT in loop)?

조회 수: 16 (최근 30일)
n33
n33 2019년 4월 5일
댓글: Walter Roberson 2023년 5월 11일
If I have the following code, how to display x(10) after x(9999) causing an error?
x = ones(1, 10);
try
x(1)
x(9999)
x(10)
catch ME
fprintf(ME.message)
end
The output I got is:
ans =
1
Index exceeds the number of array elements (10).
Thanks!

채택된 답변

Walter Roberson
Walter Roberson 2019년 4월 5일
x = ones(1,10);
idx = [1 9999 10];
for K = 1 : length(idx)
try
x(K)
catch ME
fprintf(ME.message)
end
end
  댓글 수: 8
Steven Lord
Steven Lord 2023년 5월 11일
A = command1thatcanpossiblycauseanerror;
B = command2thatcanpossiblycauseanerror;
C = command3thatcanpossiblycauseanerror;
D = command4thatcanpossiblycauseanerror;
% etc
That has a similar code smell to the frequently asked question about dynamically creating variables with numbered names like x1, x2, x3, etc., for which the general consensus is to avoid doing that.
Rather than posing a hypothetical scenario, can you share a little more detail about your actual application where you use a pattern like that code? What do those commands that can possibly throw an error do? Are they independent or are the outputs of one or more of those functions the inputs to later of those functions?
Walter Roberson
Walter Roberson 2023년 5월 11일
I am having difficulty thinking of any code that is such that
A = command1thatcanpossiblycauseanerror;
is valid code, but
for dfpSzcGKKqyMbgIcvYXqkicEiRRKtIUpb_vjDshBQupYrSfhm_GS_xNidaex = 1 : 1
A = command1thatcanpossiblycauseanerror;
end
"cannot" work.
The only things I can come up with are along the lines of "needing" dbstack to be able to pinpoint the function name... ummm, no, even that would not explain things.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by