How can I add a backup an option, addSaveCallback, to save a timestamped version of my file, to the MATLAB editor?
75 次查看(过去 30 天)
显示 更早的评论
% 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
3 个评论
dpb
2025-8-29,14:53
编辑:dpb
2025-8-29,16:06
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'.
>>
回答(4 个)
Yair Altman
about 6 hours 前
编辑:Yair Altman
about 6 hours 前
I believe that you can use the editor object's undocumented (hidden) ContentSaved event, as follows:
documentObj = matlab.desktop.editor.getActive;
hListener = addlistener(documentObj, 'ContentSaved', @mySaveCallback);
function mySaveCallback(hDocument, hEventData)
savedFilePath = hDocument.Filename;
saveBackup(savedFilePath);
end
For the record, here is how you can see similar undocumented (hidden) events of a Matlab object:
obj = matlab.desktop.editor.getActive;
mc = metaclass(obj);
events = mc.EventList;
eventNames = {events.Name};
eventAccess = {events.ListenAccess};
eventHidden = [events.Hidden];
[{'Event name','Access','Hidden'}; ...
sortrows([eventNames; eventAccess; arrayfun(@mat2str,eventHidden,'Uniform',false)]')]
and the output in this specific case would be:
ans =
4×3 cell array
{'Event name' } {'Access'} {'Hidden'}
{'ContentDirtied' } {'public'} {'true' }
{'ContentSaved' } {'public'} {'true' }
{'ObjectBeingDestroyed'} {'public'} {'false' }
0 个评论
dpb
2025-8-29,14:49
编辑:dpb
2025-8-29,15:58
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 个评论
dpb
2025-8-29,15:04
编辑:dpb
2025-8-29,15:10
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.
Matt J
2025-8-29,16:19
编辑:Matt J
2025-8-29,16:19
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 个评论
Steven Lord
about 5 hours 前
1 个评论
dpb
about 5 hours 前
编辑:dpb
about 2 hours 前
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!