How can I add a backup an option, addSaveCallback, to save a timestamped version of my file, to the MATLAB editor?

50 ビュー (過去 30 日間)
Mark
Mark 2025 年 8 月 29 日 9:40
コメント済み: dpb 2025 年 8 月 31 日 15:02
% 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
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'.
>>
Mark
Mark 2025 年 8 月 29 日 16:19
Ah yes the code looks plausible. Certainly the function saveBackup() should work, but
editorObj.addSaveCallback(@mySaveCallback); is not a real function.
An AI invented answer to my query I think.
Thanks for taking a look at the problem.

サインインしてコメントする。

回答 (2 件)

dpb
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
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.
Mark
Mark 2025 年 8 月 29 日 16:15
Thanks for having a go at this
I was hoping I had missed something but I agree with you that there are no obvious onClose or onSave callbacks exposed for the user. I'll submit an enhancement request as you suggest.
The PC I want to run this on is so heavily locked down, I can't run a local version control such as git.

サインインしてコメントする。


Matt J
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 件のコメント
Mark
Mark 2025 年 8 月 30 日 18:55

I like the custom save button or keyboard shortcuts options. I'll try those and report back.

dpb
dpb 2025 年 8 月 31 日 15:02
Would be perfect if the keybindings would allow to overrule the present Ctrl-S

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDesktop についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by