How can I get plain text diary files?

22 次查看(过去 30 天)
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 个评论
FM
FM 2021-8-31
编辑:FM 2021-8-31
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')
ETHAN
ETHAN 2024-9-3
Thanks @FM this seems to be the most straightforward solution for this without changing the startup

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-5-6
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 个评论
Jeff Miller
Jeff Miller 2018-5-7
Your speculation is spot on: starting with either of those switches turns off the formatting.

请先登录,再进行评论。

更多回答(3 个)

Steven Lord
Steven Lord 2018-6-7
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 个评论
Walter Roberson
Walter Roberson 2018-6-7
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 2018-6-8
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.

请先登录,再进行评论。


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

请先登录,再进行评论。


Bruce Elliott
Bruce Elliott 2018-6-7
编辑:Bruce Elliott 2018-6-7
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 个评论
Walter Roberson
Walter Roberson 2018-6-7
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Testing Frameworks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by