Stuart’s MATLAB Videos

Watch and Learn

Using try/catch in a While Loop to Repeatedly Execute MATLAB Code 3

Posted by Stuart McGarrity,

You may be familiar with using try/catch statements to make your code robust to errors. Matt covered them in a series of posts. A try/catch statement will catch one occurrence of an error in the try section and then continue to the catch. But what if you want to keep trying the same piece of code until it works? This video shows how to do this by inserting the try/catch statement in a while loop.

You can skip the section from 6:45 to 9:20, labeled in the table of contents “Breakpoint Not Working” to “Continue Stepping Through tryfunc” where I struggle to understand why something is not working. I eventually explain what is going on @15:45, labeled “Explanation of Breakpoint not working” in the table of contents.

Play the video in full screen mode for a better viewing experience.

3 CommentsOldest to Newest

Jan replied on : 1 of 3
Hello Stuart, Thanks for new video and (bit late) Happy Birthday! From my experience, it's not only MATLAB having such problems with different paths to the same file. Anyway, having in mind one of your previous videos about little useful tools, I've edited your code to track files which aren't on current search path or are hidden, closes them and opens their filename equivalents from current path.
function reopennonpathfiles
% Close open files being not under search path (or currently hidden)
% and reopen correct ones in editor

%% Get Files
allOpenFiles = matlab.desktop.editor.getAll'; % Array of open files
filePaths = {allOpenFiles.Filename}'; % Extract file names

%% Get Current Searth Path Paths To Files With Same Filenames
filenames = regexprep(filePaths,'.*[\\/]',''); % remove all except filename
pathPaths = cellfun(@which, filenames, 'UniformOutput', false)

%% Find Which Files Not Under Path
found = cellfun(@(A,B)~strcmpi(A,B), filePaths, pathPaths); % Logical array for matlabroot files

%% Close Files
% Call close method of all matlabroot files
allOpenFiles(found).close
cellfun(@matlab.desktop.editor.openDocument, pathPaths(found), 'uniformOutput', false);
It's definitely not perfect, and requires couple of things to take care of to make it works under any circumstances, but it shows the idea. Probably it would be the best to rewrite it to a function that does the same thing, but only for active editor document .
Jan replied on : 2 of 3
Here is version that works only for active file in editor. Personally I think this is a better option, cause we could have other files opened intentionally, and we may don't want them to be closed. Also this version prevents error when no such file on search path, if so, active file is just closed.
function reopennonpathfile
% Close active file if being not under search path (or currently hidden)
% and reopen correct one in editor

%% Get Active Editor File
openFilePath = matlab.desktop.editor.getActiveFilename; 
filename = regexprep(openFilePath,'.*[\\/]',''); % remove all except filename
pathPath = which(filename);

%% Check
if ~strcmpi(openFilePath,pathPath)
  %% Close File
  matlab.desktop.editor.getActive().close
  %% Open File if found
  if ~isempty(pathPath)
    matlab.desktop.editor.openDocument(pathPath);
  end
end