MATLAB Answers


Matlab doesn't write on excel after the first loop in try catch

Asked by farzad on 20 Jun 2019
Latest activity Edited by Guillaume
on 21 Jun 2019
Hi all
I use try catch to force matlab to continue the loop and in each loop I have the parameters to write to excel files. But after the first successful loop, despite the run continues, I don't see the new files be written. Is there a reason?


@ farzad, We need a MWE. You'll need to share code that reproduces the problem. If your car doesn't start, you can't take your neighbor's car to the mechanic to get it fixed.
If your code is sensitive, you can simplify it so that it recreates the problem with as few lines as possible.
for f = 1:size(Flist) b% <--- size(Flist) returns a 1x2 vector
close all
cd readfilesfolder %<--- it's better to use absolute paths to files
filename = Flist{f}; %than to change the cd within a loop
A = xlsread(filename);
cd writefilesfolder % <---- see comment above
xlswrite % <--- does it overwrite the same file each time?
mycodeagain % <--- what's happening here?
Thank you very much
well I feel changing cd in the loop could be the problem , since I had many files I wanted to avoid a mess of different files in one folder.
for the last part you were asking what's happening here please look :
I think for the try catch continue, I should include the same code under try . Am I right ?
maybe another way is rethrow ME is it ?
I really can't troubleshoot it without seeing the code. The link you provided is someone else's code.
"I feel changing cd in the loop could be the problem"
It certainly could lead to a problem but I doubt that this alone is preventing the loop from continuing.
If we can't see a version of your code that reproduces the problem, I recommend that you step through the code line by line in debug mode to figure out where the problem happens.

Sign in to comment.

2 Answers

Answer by Adam Danz
on 20 Jun 2019
Edited by Adam Danz
on 20 Jun 2019
 Accepted Answer

Without having much information, your error is probably due to improper loop setup.
for f = 1:size(Flist)
size(Flist) probably returns a [1 x n] vector which would result in only 1 iteration of the for-loop. If Flist is a list of files you probably want
for f = 1:numel(Flist)


Thank you so much , I have tried it despite the f was counting to the write number anyway, I think part of the problem is due to changing folder inside the loop
If the loop was continuing, then 'Flist" must have been a column vector or an nxm array where n>1. In any case, you don't want to use size() in that line of code. numel() is what you want.

Sign in to comment.

Answer by Guillaume
on 21 Jun 2019
Edited by Guillaume
on 21 Jun 2019

There's never any reason to use cd and using cd is matlab is dangerous since it can change which m files are in scope.
It's double dangerous to use with try... catch statements if don't track which directory you are in. You may be expecting to be in the readfolder directory but are actually still in the writefolder directory, or vice-versa.
Not using cd doesn't mean you have to have everything in the same folders. You can still use as many folders as you want. All matlab functions also work with absolute paths as weel as relative paths, so the code would be something like:
readfolder = 'C:\somewehere\somefolder\toreadfrom';
writefolder = 'C:\somewhere\somefilder\towriteto';
for ...
readfile = fullfile(readfolder, Flist{f}); %build absolute path of file to read
A = xlsread(readfile); %A is a very poor variable name!
writefile = fullfile(writefolder, Flist{f}); %build absolute path of file to write
xlswrite(writefile, A);
On a similar note, close all is also not good practice. You should track which figures you have created and close only these.


Sign in to comment.