How to publish results from the command window in MATLAB
Afficher commentaires plus anciens
Hi,
I ran a code it took 5 hours to give me results in the command window now I want to print the results from the command window as a pdf whenever I try to click on print then choose microsoftprint to pdf. The output file is not a pdf file!
Réponse acceptée
Plus de réponses (2)
James Johnson
le 19 Déc 2019
Modifié(e) : James Johnson
le 21 Déc 2019
I found a solution that not only preserves formatting it may allow programatic printing of the command window contents by use of a FEX file: "Print with Internet Explorer".
The .m file below works on small scale testing with tables featuring uneven headings. It basically just makes an html file out of the diary file.
Three notes: 1) HTML does not like whitespace characters so I use " " which is not ideal. 2) I set the font to monospace without a CSS header, that also may not be ideal. 3) to print the html file to PDF or to paper carefully read Jan's comments and answer here: https://www.mathworks.com/matlabcentral/answers/376787-how-to-print-a-web-page-automatically
diary('diary_file.txt')
diary on
% NOTE: replace the next 4 lines (not including "diary off") with your own code
a=[1;0];
bbbbb=[0;rand];
% example of persnickety command window formatting
test_table=table({'var1';'var2_extra'},a,bbbbb)
diary off
%% NOTE: ALL the following lines could be in a separate file
% open the file to get the content as a string
fid = fopen('diary_file.txt','r');
f=fread(fid,'*char')';
fclose(fid);
% adapt to html
f=strrep(f,'\','\\',); % thanks to mahoromax's comment (accomodates windows file paths)
f=strrep(f,newline,'<br>');
f=strrep(f,' ',' ');
f=['<p style = "font-family:monospace" >',newline,f,newline,'</p>'];
% write the file and view it
fid=fopen('diary_file.html','w');
fprintf(fid,f);
fclose(fid);
winopen('diary_file.html') % windows only?
5 commentaires
This seems really neat.
But my output stops working at the first \ of a filepath. (D: is printed and then everything else (=99% of input) is lost.
The adaption to html still works. but the writing to file seems to break sth. Not corrupt html code, it just doesnt seem to arrive at the html file.
Nvm. Fprintf doesnt only use % stuff but also \
strrep(f,'\','\\',);
Does the trick (add into the adapt section.
James Johnson
le 21 Déc 2019
I added that line. There are likely to be other characters that cause trouble. Individuals will have to adapt. I'm happy to update my answer with corrections such as yours.
That code will have problems with backslashes and any other fprintf special characters, because of this line where you treat an fprintf format string as a literal string (they are not):
fprintf(fid,f);
Unless you escape all special characters in your literal string then you will always get some errors or unexpected output characters. However you can avoid all of this very simply by using a simple format string and providing a literal input string (your HTML string), i.e. like this:
fprintf(fid,'%s',f);
Note that this actually resolves the problem at its cause, unlike that very awkward strrep (which is incorrectly commented that this "bug" has something to do with windows file paths... it doesn't, it is due entirely to how fprintf was used with just the format string and would occur for any text in that format string which includes backslashes or other special characters).
Image Analyst
le 21 Déc 2019
James, I 100% agree with Stephen. You can go ahead and "update my answer with corrections such as yours."
Micheal Omojola
le 22 Déc 2020
This is awesome! Thanks James!
Paresh Lalwani
le 3 Oct 2020
0 votes
what do if my output contain graph & i want both command window output and graph as pdf.
please,help me it is urgent.
when i tried a function publish as pdf it contain only editor window code but i want my command window and graph as pdf.
1 commentaire
Tamas Galli
le 16 Déc 2020
Modifié(e) : Tamas Galli
le 16 Déc 2020
diary command does not seem to support graphics, see Section Limitations: https://www.mathworks.com/help/matlab/ref/diary.html
The output you can be read back nicely formatted with the type command in Matlab from where you can use copy&paste.
Catégories
En savoir plus sur File Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

