How can I get plain text diary files?

27 visualizaciones (últimos 30 días)
Jeff Miller
Jeff Miller el 6 de Mayo de 2018
Editada: FM el 31 de Ag. de 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 comentarios
Jeff Miller
Jeff Miller el 6 de Mayo de 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 el 31 de Ag. de 2021
Editada: FM el 31 de Ag. de 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')

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 6 de Mayo de 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 comentario
Jeff Miller
Jeff Miller el 7 de Mayo de 2018
Your speculation is spot on: starting with either of those switches turns off the formatting.

Iniciar sesión para comentar.

Más respuestas (3)

Steven Lord
Steven Lord el 7 de Jun. de 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 comentarios
Walter Roberson
Walter Roberson el 7 de Jun. de 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 el 8 de Jun. de 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.

Iniciar sesión para comentar.


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

Iniciar sesión para comentar.


Bruce Elliott
Bruce Elliott el 7 de Jun. de 2018
Editada: Bruce Elliott el 7 de Jun. de 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 comentario
Walter Roberson
Walter Roberson el 7 de Jun. de 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.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by