MATLAB Answers

Compiled application does not write to diary log file

21 views (last 30 days)
MC
MC on 16 Jan 2020
Commented: MC on 17 Jan 2020
Hi all,
I have created a project using app designer and a number of functions. To keep track of how the calculation routes through the various functions I save comments in a log file using the diary() function and many fprintf() functions (see simplified example code below). This all works perfectly when run through matlab.
However, when I run a compiled version of the project, the log file is completely empty, even though the log file itself is created. (other files are also created/saved correctly).
I assume it's because there is no command window... for fprintf() to write to...? The same happens on my colleagues system.
Is there a way around this? Or do I need to write a dedicated logging function?
TIA, Mark.
R2019b
function mlappGUI()
main()
end
function main()
diary('c:\MatLab\logFile.log') %starts the log file
sub1()
sub2()
diary off
end
function sub1()
fprintf('subFunction1 : log file comment\n');
end
function sub2()
fprintf('subFunction2 : log file comment\n');
end

  0 Comments

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 16 Jan 2020
I think you are right. The cause is that there is no Command Window in your compiled app.
I would suggest writing to a specific file. Use fid=fopen() to create a FID and then fprintf(fid,'log text')
You can switch to Command Window when forcing fid to be 1.
fprintf(1,'abc\n') is the same as display('abc')

  3 Comments

MC
MC on 17 Jan 2020
Hi Fangjun Jiang,
Thanks for the response.
I have reworked it to look something like this and used a single global variable as I don't pass lots of data into some of the smaller functions.
function main()
global logID
logID = fopen('C:\matlab\Log.log');
fprintf(logID,'main: blah %i\n',0);
sub1()
fclose(logID)
end
function sub1()
global logID
fprintf(logID,'sub1: blah %i\n',1);
end
The only problem is that fprintf() doesn't output to the command window (which is useful for development).
A possible solution may be to create a dedicated logging function which outputs to both:
function main()
global logID
logID = fopen('C:\matlab\Log.log');
logEntry(sprintf('main: blah %i\n',0))
sub1()
fclose(logID)
end
function sub1()
logEntry(sprintf('sub1: blah %i\n',1))
end
function logEntry(txt)
global logID
fprintf(txt); % display in command window
fprintf(logID,txt); % save to log file.
end
Fangjun Jiang
Fangjun Jiang on 17 Jan 2020
When you debug during development, force logID to be 1 somewhere then it will print to the Command Window. Run diary() outside of your app to save the log into a file.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2019b

Translated by