Use diary off with Closerequestfcn

11 次查看(过去 30 天)
PierC
PierC 2022-12-1
回答: Abhinav Aravindan 2024-12-11,11:39
Hello everybody,
I am trying to incorporate the diary off command within the Close RequestFcn expression, but I always get an error, here the details:
The idea: I open a figure and then start to record the command window. Then, if I manually close the figure by pressing on the x at the top right corner of the figure window, the command window displays a text and, at the same time, the diary stops recording.
Here is the code that does not work:
fig = figure();
diary write.txt;
fig.CloseRequestFcn = @(~,~){fprintf('Script aborted\n'); close(fig, 'force'); diary('off')};
Here is the error:
Error using diary
Too many output arguments.
Error in Temp4>@(~,~){fprintf('Script aborted\n');close(fig,'force');diary('off')} (line 5)
fig.CloseRequestFcn = @(~,~){fprintf('Script aborted\n'); close(fig, 'force'); diary('off')};
Error while evaluating DestroyedObject CloseRequestFcn.
Note: if I remove "diary off" from the expression it works fine, but then I cannot delete write.txt file until I manually type "diary off" in the command window.
I hope I managed to explain myself. Really thank you in advance to everyone for the help!

回答(1 个)

Abhinav Aravindan
Abhinav Aravindan 2024-12-11,11:39
Hi @PierC,
A similar issue to yours has been addressed in the following link:
To resolve the issue, you may instead define a seperate function to implement the required operations as follows:
fig = figure();
diary('write.txt');
fig.CloseRequestFcn = @(src, event) closeFigureFcn(src);
function closeFigureFcn(fig)
fprintf('Script aborted\n');
diary('off');
delete(fig);
end
Please refer to the below documentation for further detail:

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by