MATLAB Answers

0

How can I get plain text diary files?

Asked by Jeff Miller on 6 May 2018
Latest activity Commented on by Jeff Miller on 8 Jun 2018
I am trying to use diary() to save unit test output in plain text files for later checking. The output is pretty hard to read, though, because of embedded markup. For example:
--> Failure table:
<strong>Index</strong><strong> Actual </strong><strong> Expected</strong><strong> Error </strong><strong> RelativeError</strong><strong> AbsoluteTolerance</strong><strong> RelativeTolerance</strong>
<strong>_____</strong> <strong>________</strong> <strong>________</strong> <strong>________</strong> <strong>_____________</strong> <strong>_________________</strong> <strong>_________________</strong>
3 1.704375 1.5 0.204375 0.13625 0.005 0.005
Is there some way to turn off this markup, or some more convenient way to save unit test output to a file that I can read in a plain-text editor? I'd like to get the same output that is produced by copying and pasting from the command window, which looks like this (but of course I want to get it programmatically):
--> Failure table:
Index Actual Expected Error RelativeError AbsoluteTolerance RelativeTolerance
_____ ________ ________ ________ _____________ _________________ _________________
3 1.704375 1.5 0.204375 0.13625 0.005 0.005
Thanks,

  2 Comments

Yes, I am using the testing framework. It was actually producing nice plain text diary files ... until my uni went to a newer version of MATLAB.

Sign in to comment.

4 Answers

Answer by Walter Roberson
on 6 May 2018
 Accepted Answer

You appear to be using the Testing Framework.
When I chase through the code, it looks to me as if there underlying code deliberately carries around two versions of the text, one with rich text / hyperlinking, and one without.
I did not manage to quite follow how which one gets chosen in toolbox/matlab/testframework/unittest/core/+matlab/+unittest/+internal/+diagnostics/TableDiagnostic.m -- I suspect it might be an implicit get() that is activating the choice.
The relevant code makes use of the built-in matlab.unittest.internal.richFormattingSupported which is undocumented and cannot be looked at.
I speculate that you might be able to turn off the formatting by starting MATLAB with -nodesktop or perhaps even -nojvm .

  1 Comment

Your speculation is spot on: starting with either of those switches turns off the formatting.

Sign in to comment.


Answer by Steven Lord
on 7 Jun 2018

Consider running your tests via a test runner that uses a plugin to output the data to a file.
For more information see the TAPPlugin class, the ToFile class, and/or the ToUniqueFile class. The TAPPlugin class documentation page doesn't have an example, but it is used in the examples on the ToFile and ToUniqueFile pages.

  2 Comments

I did see this when I looked at it before, but it looked like there were only two choices, one of which put in the bold stuff, and the other of which required you to write the entire plug-in code yourself. I did not notice any utility routines or customization, certainly nothing like a style sheet or options structure.
Thanks, Steven, but that looks like overkill for my purposes. I must admit I was hoping for a simple solution like
diary('filename',false)
where false would turn off the formatting.

Sign in to comment.


Answer by per isakson
on 6 May 2018

Until something better turns up try
ffs = 'c:\tmp\dbch_diary.txt';
str = fileread( ffs );
out = regexprep( str, '<\x2F?strong>', '' );
It removes "<strong>" and "</strong>". \x2F is hex for literal backslash.
I believe that the command window uses only a small subset of HTML. Hopefully, a small number of regexprep-statements, added as needed, will do the job.

  1 Comment

Thanks for this very practical suggestion. Walter's startup switch idea seems slightly better since it eliminates the formatting.

Sign in to comment.


Answer by Bruce Elliott on 7 Jun 2018
Edited by Bruce Elliott on 7 Jun 2018

Here's another solution, from Walter Roberson's answer to a similar question that I had posed ( How to capture lines written by disp()? ). I had been asking about tables in general, but since the testing framework produces a table as output, I think his response applies here as well.
Walter's suggestion was to use evalc() to capture the output of disp(), and to use the second argument of the disp method for tables to turn off the bold formatting. It looks something like this:
result = evalc('disp(myTable,false)');
fwrite(fid,result);
It works like a charm.

  1 Comment

Hmmm, when I looked at this earlier, it looked to me as if the testing framework does not directly produce tables as output: it appeared to be outputting under control, with the user not having direct access to the place the output was displayed.

Sign in to comment.