{"id":2217,"date":"2016-04-20T11:26:12","date_gmt":"2016-04-20T16:26:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/videos\/?p=2217"},"modified":"2016-05-23T12:44:51","modified_gmt":"2016-05-23T17:44:51","slug":"creating-a-utility-to-close-all-open-files-in-the-matlab-editor-under-matlabroot","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/videos\/2016\/04\/20\/creating-a-utility-to-close-all-open-files-in-the-matlab-editor-under-matlabroot\/","title":{"rendered":"Creating a Utility to Close All MATLAB Base Functions Open in the Editor"},"content":{"rendered":"<p>When your MATLAB code encounters an error and you have &#8220;dbstop if error&#8221; 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.<\/p>\n<p>This video uses the <a href=\"https:\/\/blogs.mathworks.com\/videos\/2015\/10\/29\/matlab-code-along-videos\/\">code-along<\/a> style.<\/p>\n<p><div class=\"row\"><div class=\"col-xs-12 containing-block\"><div class=\"bc-outer-container add_margin_20\"><videoplayer><div class=\"video-js-container\"><video data-video-id=\"4842334023001\" data-video-category=\"blog\" data-autostart=\"false\" data-account=\"62009828001\" data-omniture-account=\"mathwgbl\" data-player=\"rJ9XCz2Sx\" data-embed=\"default\" id=\"mathworks-brightcove-player\" class=\"video-js\" controls><\/video><script src=\"\/\/players.brightcove.net\/62009828001\/rJ9XCz2Sx_default\/index.min.js\"><\/script><script>if (typeof(playerLoaded) === 'undefined') {var playerLoaded = false;}(function isVideojsDefined() {if (typeof(videojs) !== 'undefined') {videojs(\"mathworks-brightcove-player\").on('loadedmetadata', function() {playerLoaded = true;});} else {setTimeout(isVideojsDefined, 10);}})();<\/script><\/div><\/videoplayer><\/div><\/div><\/div><\/p>\n<p>Play the video in full screen mode for a better viewing experience.<\/p>\n<p>Function is here:<\/p>\n<p>&nbsp;<\/p>\n<pre><code>\r\nfunction closematlabrootfiles\r\n% Close files under matlabroot that are open in the editor\r\n\r\n%% Get Files\r\nallOpenFiles = matlab.desktop.editor.getAll'; % Array of open files\r\nfileNames = {allOpenFiles.Filename}'; % Extract file names\r\n\r\n%% Find Files Under MATLAB Root\r\nfoundCell = strfind(fileNames,matlabroot);\r\nfound = cellfun(@(x) ~isempty(x), foundCell); % Logical array for matlabroot files\r\n\r\n%% Close Files\r\n% Call close method of all matlabroot files\r\nallOpenFiles(found).close\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"thumbnail thumbnail_asset asset_overlay video\"><a href=\"https:\/\/blogs.mathworks.com\/videos\/2016\/04\/20\/creating-a-utility-to-close-all-open-files-in-the-matlab-editor-under-matlabroot\/?dir=autoplay\"><img decoding=\"async\" src=\"https:\/\/cf-images.us-east-1.prod.boltdns.net\/v1\/static\/62009828001\/7ddef1d3-b7d8-45e5-b9fb-6ab08b5274d5\/526d9ff0-eb8a-4586-a259-234316ace8d4\/160x90\/match\/image.jpg\" onError=\"this.style.display ='none';\"\/><\/p>\n<div class=\"overlay_container\">\n      <span class=\"icon-video icon_color_null\"><time class=\"video_length\">21:39<\/time><\/span>\n      <\/div>\n<p>      <\/a><\/div>\n<p>When your MATLAB code encounters an error and you have &#8220;dbstop if error&#8221; enabled, the file in which the error occurs is opened in the editor (together with its calling files). When the&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/videos\/2016\/04\/20\/creating-a-utility-to-close-all-open-files-in-the-matlab-editor-under-matlabroot\/\">read more >><\/a><\/p>\n","protected":false},"author":133,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[27,4],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/2217"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/users\/133"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/comments?post=2217"}],"version-history":[{"count":16,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/2217\/revisions"}],"predecessor-version":[{"id":2254,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/2217\/revisions\/2254"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/media?parent=2217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/categories?post=2217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/tags?post=2217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}