Use diary off with Closerequestfcn

11 views (last 30 days)
PierC
PierC on 1 Dec 2022
Answered: Abhinav Aravindan on 11 Dec 2024 at 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!

Answers (1)

Abhinav Aravindan
Abhinav Aravindan on 11 Dec 2024 at 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:

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!