How can I get plain text diary files?
22 views (last 30 days)
Show older comments
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,
4 Comments
FM
on 31 Aug 2021
Edited: FM
on 31 Aug 2021
Unfortunately, by 2019a at least, there are a diversity of tags, including some for hyperlinks. It makes it very hard to look through m-file output. It seems that an option to disable tagging in diary files should be straightforward (though I am not a developer).
The command "type DiaryFile.txt" renders the content into readable text in the command window. I don't suppose that something could be done with the following to capture only the visible text and excise the tagging? I mean something simple. If an m-file program is needed to use complex regular expresssions, it's not what I had in mind. Plus, it's not excising the tagging at the source, so it can easily get things wrong.
DiaryText = evalc('type DiaryFile.txt')
Accepted Answer
Walter Roberson
on 6 May 2018
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 .
More Answers (3)
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
Walter Roberson
on 7 Jun 2018
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.
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.
Bruce Elliott
on 7 Jun 2018
Edited: 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
Walter Roberson
on 7 Jun 2018
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.
See Also
Categories
Find more on Testing Frameworks 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!