Created
August 25, 2021 11:25
-
-
Save mykappa/182264ec66bfb3204a9a9fabfafe6dcc to your computer and use it in GitHub Desktop.
Save a list of MATLAB scripts and functions open in the editor in case MATLAB crahes (for people like me who always work with way too many tabs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Save list of files opened in MATLAB editor | |
% Get a list of open files | |
x = matlab.desktop.editor.getAll; | |
editor_open_files_list = {}; | |
for ii = 1:size(x,2) | |
editor_open_files_list=[editor_open_files_list; {x(1,ii).Filename} {x(1,ii).Text}]; %#ok<AGROW> | |
end | |
% Define backup path | |
current_user = getenv('USERNAME'); % PARAMETER | |
backup_path = ['C:\Users\',current_user,'\Documents\MATLAB\editor_files_list_backup\']; % PARAMETER | |
% Save file list as .mat file with current time and date | |
save([backup_path,datestr(now,'yyyymmdd_HHMMSS'),'_editor_open_files.mat'],'editor_open_files_list') % PARAMETER | |
% Limit number of backups in one day | |
max_num_backups_today = 10; % PARAMETER | |
backups_list_today = dir(['.\editor_files_list_backup\',datestr(now,'yyyymmdd_'),'*_editor_open_files.mat']); % PARAMETER | |
num_backups_today = size(backups_list_today,1); | |
% Delete oldest backup(s) of today | |
if num_backups_today > max_num_backups_today | |
for ii = 1:(num_backups_today-max_num_backups_today) | |
delete(fullfile(backups_list_today(ii).folder,backups_list_today(ii).name)) | |
end | |
end | |
% Limit total number of backups | |
max_num_backups_total = 50; % PARAMETER | |
backups_list_total = dir([backup_path,'*_editor_open_files.mat']); % PARAMETER | |
num_backups_total = size(backups_list_total,1); | |
% Delete oldest backup(s) | |
if num_backups_total > max_num_backups_total | |
for ii = 1:(num_backups_total-max_num_backups_total) | |
delete(fullfile(backups_list_total(ii).folder,backups_list_total(ii).name)) | |
end | |
end | |
clearvars |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To reopen the backed up files, load the desired backup file and run
to open all files and display a warning if an error occurs, such as a file cannot be found.