Can you specifiy an action when MATLAB throws an error

조회 수: 2 (최근 30일)
Ali Almakhmari
Ali Almakhmari 2022년 7월 26일
답변: David Hill 2022년 7월 26일
I am working on a very complex project (code) with limited memory on my laptop. There is a gigantic variable in my for loop that increases in size with each iteration until MATLAB throws me an error with no enough memory and that the variable is too big. Now my question is: how can I specify a certain action when MATLAB throws that error my way. For example, when I get that error, I want MATLAB to clear that gigantic variable and restart the for loop. Is this possible? If it is, can you give me few lines of code to achieve this? Thanks in advance.
  댓글 수: 1
dpb
dpb 2022년 7월 26일
There's try, catch that lets you continue after an error, but given the way the Q? is phrased it sounds as though you may well be dynamically reallocating the array each iteration as well. I'd suggest preallocating an array of some maximum size you know you can allocate and then allocate into it on each iteration instead; then you can/will know when you reach that array size and can take action before the fatal crash.

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

채택된 답변

David Hill
David Hill 2022년 7월 26일
while %whatever you want
for k=1:100
try
%calculation leading to gigantic variable
catch
clear gigantic
break;%restarts for-loop
end
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by