Stuart’s MATLAB Videos

Watch and Learn

Creating a Utility to Close All MATLAB Base Functions Open in the Editor 3

Posted by Stuart McGarrity,

When your MATLAB code encounters an error and you have “dbstop if error” enabled, the file in which the error occurs is opened in the editor (together with its calling files). When the error occurs inside MATLAB base functions, located in the MATLAB installation directory, they too will open in the editor. It is convenient that this happens and assists debugging, but over time this can result in an accumulation of open files that can clutter your editor. Here, I write a utility that clears out these files. I use the function matlabroot to determine the MATLAB installation folder.

This video uses the code-along style.

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

Function is here:

 


function closematlabrootfiles
% Close files under matlabroot that are open in the editor

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

%% Find Files Under MATLAB Root
foundCell = strfind(fileNames,matlabroot);
found = cellfun(@(x) ~isempty(x), foundCell); % Logical array for matlabroot files

%% Close Files
% Call close method of all matlabroot files
allOpenFiles(found).close

3 CommentsOldest to Newest

Jan replied on : 1 of 3
Hi Stuart, thanks for another interesting video. I like the code-along style of your videos, it increases teaching value of those. One thing that came to my mind is to use "strncmp" instead of "strfind" and "cellfun" in this case, so it would look like: %% Find Files Under MATLAB Root found = strncmp(fileNames, matlabroot, length(matlabroot)); % Logical array for matlabroot files Very useful and simple function. I must admit I would used it, but was too lazy to try to write something like this.
Stuart McGarrity replied on : 2 of 3
Hi Jan, Yes, strncmp would be better. I had forgotten about that function. I often use it in another utility function: function result= startswith(x,y) result = strncmp(x,y,length(y)); % true if x=[y, '***'], end
Jan replied on : 3 of 3
Ha, got the same function, but under the name strscmp :D I believe it's the curse of prosperity. Matlab built-in functions base grows and grows, and we forget about the functions we used yesterday. That's the reason I appreciate the documentation so much.