forcing try catch loop during using another script

Hello People
I have a problem in try catch loop,
I have number of files and I analyze these files by calling another script which has the analysis code.
for example let's say in the main script I am writting as the following:
num_files=20
[filename, pathname] = uigetfile([strPathLife ... '\...\*.xlsx;*.xlsm'], .. 'Excel-File auswählen',... 'MultiSelect','on') %this mean the first fiel would be file(1), file(2),... etc
for i=num_files
try
analysis_code(file(i)) % analysis_code is another script analyzes the data of the files
catch
disp('error')
end
end
according to this code it should analyze all the file disregarding if the there is error in analysing one file or more.
Thanks in advance
Ayman Mounir

Antworten (1)

chrisw23
chrisw23 am 14 Aug. 2019

0 Stimmen

The analyze script should return a result code to be evaluated. To catch any analyze fail use the assert function. Error codes can be defined in the analyze function.
i.e. (user defined exceptions)
num_files=20
[filename, pathname] = uigetfile([strPathLife ... '\...\*.xlsx;*.xlsm'], .. 'Excel-File auswählen',... 'MultiSelect','on') %this mean the first fiel would be file(1), file(2),... etc
for i=num_files
try
msg = analysis_code(file(i)) % analysis_code is another script analyzes the data of the files
assert(strcmp(msg,'No Error'),msg); % default msg = 'No Error'
catch ex
disp(ex.message)
end
end
Hope it helps
Best regards Christian

4 Kommentare

Hello christian
Thanks for your answer the problem when there is an error in analyzing one file the code should try the next file until it finshed from the analyzing all the uploaded files.
My opinion is the try catch function is only local code I mean if i am calling another matlab file and it has an error the whole process will be stopped.
Regards
Ayman
ayman mounir
ayman mounir am 19 Aug. 2019
Bearbeitet: Adam Danz am 20 Aug. 2019
for more clarification
num_files=20
[filename, pathname] = uigetfile([strPathLife ... '\...\*.xlsx;*.xlsm'], .. 'Excel-File auswählen',... 'MultiSelect','on') %this mean the first fiel would be file(1), file(2),... etc
for i=num_files
try
msg = analysis_code(file(i)); % if there is an error inside %analysis
% code during analyzing file(1) the proccess will be %stopped.
% the code should try file(2)
assert(strcmp(msg,'No Error'),msg); % default msg = 'No Error'
catch ex
disp(ex.message)
end
end
Why call assert()? Assert() generates an error and you're trying to avoid the error by the try/catch, right? Try removing assert() and see if the problem is fixed.
Walter Roberson
Walter Roberson am 20 Aug. 2019
Bearbeitet: Walter Roberson am 20 Aug. 2019
"My opinion is the try catch function is only local code I mean if i am calling another matlab file and it has an error the whole process will be stopped."
No, that is not correct. try/catch can trap error() or throw() calls generated by MATLAB routines any number of call levels below.
There are a small number of things that come to mind that try/catch cannot handle, which will abort all of the levels of calls
  1. out of memory
  2. control-C
  3. process being killed
  4. operating system process abort due to bad pointer (relevant for calling mex and loadlibrary() routines)
Also, try/catch cannot detect problems in external executable started with system() or dos() .

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2015b

Gefragt:

am 13 Aug. 2019

Bearbeitet:

am 20 Aug. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by