How can I get plain text diary files?

24 vues (au cours des 30 derniers jours)
Jeff Miller
Jeff Miller le 6 Mai 2018
Modifié(e) : FM le 31 Août 2021
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,
  3 commentaires
Jeff Miller
Jeff Miller le 6 Mai 2018
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.
FM
FM le 31 Août 2021
Modifié(e) : FM le 31 Août 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')

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 6 Mai 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 .
  1 commentaire
Jeff Miller
Jeff Miller le 7 Mai 2018
Your speculation is spot on: starting with either of those switches turns off the formatting.

Connectez-vous pour commenter.

Plus de réponses (3)

Steven Lord
Steven Lord le 7 Juin 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 commentaires
Walter Roberson
Walter Roberson le 7 Juin 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.
Jeff Miller
Jeff Miller le 8 Juin 2018
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.

Connectez-vous pour commenter.


per isakson
per isakson le 6 Mai 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 commentaire
Jeff Miller
Jeff Miller le 7 Mai 2018
Thanks for this very practical suggestion. Walter's startup switch idea seems slightly better since it eliminates the formatting.

Connectez-vous pour commenter.


Bruce Elliott
Bruce Elliott le 7 Juin 2018
Modifié(e) : Bruce Elliott le 7 Juin 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 commentaire
Walter Roberson
Walter Roberson le 7 Juin 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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by