How do I do this without using try/catch?
显示 更早的评论
I am using waitbar within Run_pushbutton_Callback (GUIDE GUI):
function Run_pushbutton_Callback(hObject, ~, handles)
try
wb = waitbar(0,'Computing...','Name','MyProgName');
wbch = allchild(wb);
jp = wbch(1).JavaPeer;
jp.setIndeterminate(1);
% call main function
handles.mainfunc(S); % S is structure of variables
close(wb); % close dialog box
catch ME
close(wb); % close dialog box
errorMessage=sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(2, '%s\n', errorMessage);
uiwait(errordlg(errorMessage)); % error dialog
% rethrow(ME); % rethrow the whole error message in command window (if needed)
end
guidata(hObject, handles);
When mainfunc() throws an error, waitbar still continues blinking - without try/catch. I used try/catch solution to close it. However, Matlab documentation says that the memory optimisation etc is not supported within try/catch: https://www.mathworks.com/help/matlab/matlab_prog/avoid-unnecessary-copies-of-data.html
Also, it looks a bit awkward to use try/catch for entire program.
- How can I close waitbar after error without using try/catch?
- How come that in-place or other optimisation does not work within a function and its subfunctions (not nested) if the function is within try/catch block?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 App Building 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!