How can i show multiple errors in one msgbox or errordlg ?

2 次查看(过去 30 天)
I have 10 types of errors and i use errordlg function to show them ,but it shows one by one popup...
i want to show these errors in one window or one msgbox...
please help anyone i am new in matlab...
Thanku....!

采纳的回答

Voss
Voss 2022-12-30
You can store the error messages and pass them to errordlg when you want to report them.
For example, let's say you have a scalar variable "x" that must be a positive integer greater than 10. We'll split those conditions into three separate error checks, and report the checks that failed.
% define the error messages:
% three types of errors can occur in this example
error_msg = { ...
'x must be positive' ...
'x must be an integer' ...
'x must be greater than 10' ...
};
% logical vector saying which error(s) occurred (initially all false):
is_error = false(1,numel(error_msg));
% initialize a variable to keep track of which error/message we're on:
msg_idx = 0;
% first error check:
% x being non-positive is the first type of error:
msg_idx = msg_idx+1;
if x <= 0
is_error(msg_idx) = true;
end
% second error check:
% x not being an integer is the second type of error
msg_idx = msg_idx+1;
if mod(x,1) ~= 0
is_error(msg_idx) = true;
end
% third error check:
% x not being greater than 10 is the third type of error
msg_idx = msg_idx+1;
if x <= 10
is_error(msg_idx) = true;
end
% report the errors that occurred (if any):
if any(is_error)
error_msg = ['The following error(s) occurred:' error_msg(is_error)];
errordlg(sprintf('%s\n\n',error_msg{:}));
end

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by