How can I add a backup an option, addSaveCallback, to save a timestamped version of my file, to the MATLAB editor?
Mostra commenti meno recenti
% I'd like to add the option to save backup files - earlier versions every time I save a file in the Matlab editor
editorObj = matlab.desktop.editor.getActive;
editorObj.addSaveCallback(@mySaveCallback);
function mySaveCallback(~, ~)
disp('File has been saved!');
% Add your custom code here
saveBackup(originalFilePath);
end
function saveBackup(originalFilePath)
% Check if the file exists
if ~isfile(originalFilePath)
error('The specified file does not exist.');
end
% Extract file parts
[filePath, fileName, fileExt] = fileparts(originalFilePath);
% Create a timestamp
timestamp = datestr(now, 'yyyymmdd_HHMMSS');
% Define the backup file name
backupFileName = sprintf('%s_backup_%s%s', fileName, timestamp, fileExt);
backupFilePath = fullfile(filePath, backupFileName);
% Copy the file to create a backup
copyfile(originalFilePath, backupFilePath);
fprintf('Backup saved as: %s\n', backupFilePath);
end
6 Commenti
The problem is there is no such property of the returned document object...
>> editorObj = matlab.desktop.editor.getActive;
>> editorObj.addSaveCallback(@saveCallbackFunction)
Unrecognized method, property, or field 'addSaveCallback' for class 'matlab.desktop.editor.Document'.
>>
Mark
il 29 Ago 2025
Steven Lord
il 10 Set 2025
I'd like to add the option to save backup files - earlier versions every time I save a file in the Matlab editor
I'm curious to hear more about your use case. I can think of a few scenarios where this could be part of a workflow, and I'm curious if you're in one of those scenarios or if there's one I haven't thought of.
One such scenario: Your function defines its own data, and you want to keep track of the data as you modify it from run to run so that you can later on go back and reproduce the results.
In this case, saving a copy of the whole function file seems like a bit of overkill. I would separate out the data into separate variables that you can save to a MAT-file (either inside your function or outside of your function) and then load when you want to reproduce your results.
Mark
il 1 Ott 2025
Risposta accettata
Più risposte (3)
I "know nuthink!" about using the programmatic interface but it appears it doesn't have exposed properties for callback functions as do graphics objects like axes but instead you would use addlistener similarly.
The returned handle is to the document and there are no code examples for addlisteener other than one for a figure so I'm not sure just what you would use for the events...in fact, trying this locally I find
>> editorObj = matlab.desktop.editor.getActive
editorObj =
Document with properties:
Filename: '...\MATLAB\Work\writeopeningbalance.m'
Opened: 1
Language: 'MATLAB'
Text: 'function writeopeningbalance(tD,tM,fnOB)↵...
Selection: [9.00 3.00 9.00 3.00]
SelectedText: ''
Modified: 0
ExtendedSelection: [9.00 3.00 9.00 3.00]
ExtendedSelectedText: {''}
Editable: 1
>> events(editorObj)
Events for class matlab.desktop.editor.Document:
ObjectBeingDestroyed
>>
that makes it appear there is no publicly visible event for save or saveAs
I tried Yair Altman's tool to poke at object handles for hidden/undocumented properties, but it doesn't recognize the document handle so no dice there.
2 Commenti
However, does seem like a reasonable enhancement.
Submit this to Mathworks as an official support or enhancement request at <Product Support Page>
The alternative would be to use a code repository to handle this topic instead.
Mark
il 29 Ago 2025
Instead of trying to get the standard Save button to do what you want, you can make a Quick Access Toolbar button which runs your own customized save routine, as implemented in your saveBackup() code.
6 Commenti
dpb
il 30 Ago 2025
That appears to require a mouse action to use? I couldn't see a way to bind to a keystroke in the doc although I've not ever messed around with it. Doesn't seem nearly as clean a solution as would be desirable.
Matt J
il 30 Ago 2025
There are 3rd party keyboard listeners you could use as well, e.g.,
dpb
il 30 Ago 2025
No limit to what folks have done, is there?
Unfortunately, apparently for OP is under IT's thumb and if can't get them to let use git, they're probably not going to let him download/install another add-on, either. <sad>
BTW, was just curious if had missed something, not criticizing; it would be the closest that looks like can be done at present.
I do think it strange there are no other exposed events in the document editor interface, though, if going to allow programmatic control.
Matt J
il 30 Ago 2025
Apparently, there are built-in keyboard shortcuts for Quick Access Toolbar buttons,
Mark
il 30 Ago 2025
dpb
il 31 Ago 2025
Would be perfect if the keybindings would allow to overrule the present Ctrl-S
Steven Lord
il 9 Set 2025
0 voti
5 Commenti
It will/can save a backup automagically on a periodic basis, but it rewrites to the same file name with the chosen backup extension each time, doesn't it, Steven?
OP is asking to create a running series of backups with the datetime string in the file name(*) for a permanent record instead of simply ensuring have a backup of the current working file from its present state that gets updated using the fixed choice of extension, but same base filename.
Unless there's yet another option I've missed?
(*) I'd probably choose to add the date as a second extension rather than in the base filename to make parsing a little easier, but same idea or mimic VAX VMS, it added a cycle number, ";N" on the end of the file transparently to the user. having to do anything on copy or saving changed version. The time stamp then came along for free.
Mark
il 10 Set 2025
dpb
il 16 Set 2025
I guess the issue here would be keeping track of the present verson number to increment it...the code would have to either parse the highest extant from the directory list or somehow keep a persistent/global variable with all the hassles that could entail.
Seems like a reasonable enhancement/extension to the present options, @Steven Lord? Having a function that would then mimc the PURGE command would be the cat's meow...what I recall otomh was it had switches to allow keeping a number of versions, by date (before/since), and to confirm deletion. There were undoubtedly other features I don't recall.
Steven Lord
il 16 Set 2025
Sure, sounds like a reasonable enhancement to request via Technical Support. Please be sure to describe your desired workflow (how you would use this functionality if it existed) so that the developers can take it into account when or if they design this behavior.
However, I suspect that the developers may push back on this as it seems to me like there would be significant overlap between the functionality you've described and the source control interface MATLAB already provides. So the workflow would need to provide some pretty strong motivation.
It's not neecessarily my particular workflow; @Mark provided the justification in that his environment doesn't give him access to the source control tool with which to use the interface.
The VMS-style version would/could simply be an additional option within the present backup selections that don't yield the same ability as is. I've missed it; I haven't put it into the editor for source files, but the workflow you hypothesized I handle by a script that saves the workplace variables to a named mat file including a desriptive comment with a companion tool to retrieve the last or a given comment match.
The version on m-files isn't at all a bad idea, however, without the extra of a full-blown vcs.
.I doubt it would ever get sufficient traction to be implemented, however, owing to that perception from the developers.
Categorie
Scopri di più su Entering Commands in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!