try catch loop to evaluate what contains in the folder

If I have a function evaluate the files exist in the directory. I want if there is an error in evaluation of one first file go to the next file and evaulate it until all the files are evaluated. simlpy try and catch loop.
for example:
for i=number of files
try
eval.fun(file(i))
catch ME
sprintf( ' error in %s', file.name(i))
end
end

6 Comments

"If I have a function evaluate the files..."
Unclear terminology: text can be evaluated, but files can be run (scripts) or called (functions) or imported (data). What are the files exactly: do they contain code?
let's say I have excel files and I extract data from them and do some calculations during this process could the errors happen. is it now clear ?
"let's say I have excel files and I extract data from them..."
That is importing the file data. Importing file data is nothing like evaluating code.
I recommend reading the MATLAB documentation:
"...and do some calculations during this process could the errors happen."
Most likely an if would be better than try-catch. In any case, you have not said what problems you are having, or how we can help you: the basic try-catch concept you show in your question could work (although your code seems to have various other bugs).
sorry manbe i am not clear enough,
load_files
for i=numel(file)
try
evaluction=eval(file(1)))
catch
sprintf( ' error in %s', file.(i))
end
end
this should try to perform the code for file(1) if error happens then perform the code for file(2) until the last file i have uploaded
thanks
"this should try to perform the code for file(1) if error happens then perform the code for file(2) until the last file i have uploaded"
I doubt it. You do not define file anywhere in your code, but if it is some kind of array of filenames then your code won't do anything like what you state, for the reasons that I have already explained in my previous comments. Perhaps you should read the MATLAB documentation links that I gave you.
Without knowing what load_files and file are, we have no idea what your code does.

Sign in to comment.

Answers (1)

evaluction=eval(file(1)))
We do not know what the data type is of file, but in all of the cases that come to mind, eval() does not return a value.
Also I suspect that file might be file names, in which case you are more likely to need file{i}

Categories

Find more on Programming in Help Center and File Exchange

Products

Asked:

on 9 Aug 2019

Edited:

on 13 Aug 2019

Community Treasure Hunt

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

Start Hunting!