Problem using diary with standalone application
I am developing a MATLAB app using the appdesigner (MATLAB version R2019b), which is intented to be used as standalone ‘.exe’ on Windows. For traceablity I wanted to include a log-file of all outputs. The ‘diary()’ function seemed to be a good function for this. In my code I have a lot of ‘disp(…)’ commands (also in code parts that I cannot or do not want to change). The output for these disp-commands works perfectly in the MATLAB command window when I run the application within the MATLAB environment. However, when run it as Windows standalone the behavior is not logical to me: In the WIndows cmd window the output is the same as in the MATLAB command window, but the diary file only logs the output for the ‘startupFcn()’ and the ‘UIFigureCloseRequest()’ functions and not for any callback, e.g. here from a button ‘DispHelloWorldButtonPushed()’.
Does anybody know how to solve this issue and where it comes from? Btw, also the build-in ‘Create log file’ option during compilation shows the same behavior as the ‘diary’ function.
Here is a minimal working example of an app with this behavior:
classdef Test < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DispHelloWorldButton matlab.ui.control.Button
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
diary(fullfile(pwd,’log.txt’))
diary on
disp(‘Hello World!’)
end
% Button pushed function: DispHelloWorldButton
function DispHelloWorldButtonPushed(app, event)
diary on
disp(‘Button: Hello World!’)
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
disp(‘Bye bye World!’)
diary off
delete(app)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = ‘UI Figure’;
app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
% Create DispHelloWorldButton
app.DispHelloWorldButton = uibutton(app.UIFigure, ‘push’);
app.DispHelloWorldButton.ButtonPushedFcn = createCallbackFcn(app, @DispHelloWorldButtonPushed, true);
app.DispHelloWorldButton.Position = [159 161 311 175];
app.DispHelloWorldButton.Text = ‘Disp(”Hello World!”)’;
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Test
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
endI am developing a MATLAB app using the appdesigner (MATLAB version R2019b), which is intented to be used as standalone ‘.exe’ on Windows. For traceablity I wanted to include a log-file of all outputs. The ‘diary()’ function seemed to be a good function for this. In my code I have a lot of ‘disp(…)’ commands (also in code parts that I cannot or do not want to change). The output for these disp-commands works perfectly in the MATLAB command window when I run the application within the MATLAB environment. However, when run it as Windows standalone the behavior is not logical to me: In the WIndows cmd window the output is the same as in the MATLAB command window, but the diary file only logs the output for the ‘startupFcn()’ and the ‘UIFigureCloseRequest()’ functions and not for any callback, e.g. here from a button ‘DispHelloWorldButtonPushed()’.
Does anybody know how to solve this issue and where it comes from? Btw, also the build-in ‘Create log file’ option during compilation shows the same behavior as the ‘diary’ function.
Here is a minimal working example of an app with this behavior:
classdef Test < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DispHelloWorldButton matlab.ui.control.Button
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
diary(fullfile(pwd,’log.txt’))
diary on
disp(‘Hello World!’)
end
% Button pushed function: DispHelloWorldButton
function DispHelloWorldButtonPushed(app, event)
diary on
disp(‘Button: Hello World!’)
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
disp(‘Bye bye World!’)
diary off
delete(app)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = ‘UI Figure’;
app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
% Create DispHelloWorldButton
app.DispHelloWorldButton = uibutton(app.UIFigure, ‘push’);
app.DispHelloWorldButton.ButtonPushedFcn = createCallbackFcn(app, @DispHelloWorldButtonPushed, true);
app.DispHelloWorldButton.Position = [159 161 311 175];
app.DispHelloWorldButton.Text = ‘Disp(”Hello World!”)’;
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Test
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end I am developing a MATLAB app using the appdesigner (MATLAB version R2019b), which is intented to be used as standalone ‘.exe’ on Windows. For traceablity I wanted to include a log-file of all outputs. The ‘diary()’ function seemed to be a good function for this. In my code I have a lot of ‘disp(…)’ commands (also in code parts that I cannot or do not want to change). The output for these disp-commands works perfectly in the MATLAB command window when I run the application within the MATLAB environment. However, when run it as Windows standalone the behavior is not logical to me: In the WIndows cmd window the output is the same as in the MATLAB command window, but the diary file only logs the output for the ‘startupFcn()’ and the ‘UIFigureCloseRequest()’ functions and not for any callback, e.g. here from a button ‘DispHelloWorldButtonPushed()’.
Does anybody know how to solve this issue and where it comes from? Btw, also the build-in ‘Create log file’ option during compilation shows the same behavior as the ‘diary’ function.
Here is a minimal working example of an app with this behavior:
classdef Test < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DispHelloWorldButton matlab.ui.control.Button
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
diary(fullfile(pwd,’log.txt’))
diary on
disp(‘Hello World!’)
end
% Button pushed function: DispHelloWorldButton
function DispHelloWorldButtonPushed(app, event)
diary on
disp(‘Button: Hello World!’)
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
disp(‘Bye bye World!’)
diary off
delete(app)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = ‘UI Figure’;
app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
% Create DispHelloWorldButton
app.DispHelloWorldButton = uibutton(app.UIFigure, ‘push’);
app.DispHelloWorldButton.ButtonPushedFcn = createCallbackFcn(app, @DispHelloWorldButtonPushed, true);
app.DispHelloWorldButton.Position = [159 161 311 175];
app.DispHelloWorldButton.Text = ‘Disp(”Hello World!”)’;
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Test
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end standalone, disp, diary, callback, appdesigner MATLAB Answers — New Questions