How to publish results from the command window in MATLAB

71 ビュー (過去 30 日間)
Cantor Set
Cantor Set 2019 年 10 月 21 日
コメント済み: Micheal Omojola 2020 年 12 月 22 日
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!

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 10 月 21 日
One Way:
You can use diary function to save the all command window file in text form, later do the PDF conversion
diary on
% Your Code
diary off
  2 件のコメント
James Johnson
James Johnson 2019 年 12 月 19 日
編集済み: James Johnson 2019 年 12 月 21 日
(UPDATE: I solved this problem in a 2nd answer below)
This does not preserve formatting so it will produce very ugly results (not fitting the word "publish" in the question). Example:
diary('diary_file')
diary on
a=[1;0];
b=[0;1];
test_table=table(a,b)
diary off
The content of command window looks nice:
However the "diary_file" is awful:
I have yet to find a program (other than matlab) that can display this properly formatted. A tedious work around is to manually display it in the command window and print to pdf. Unfortunately matlab does a poor job of handling line breaks. So if you don't want to wrap lines you have to adjust the font size to avoid cutting off long lines. A user-friendly program allows you to "reduce to printer margins" so that you can avoid laboriously tweaking font settings.
Paresh Lalwani
Paresh Lalwani 2020 年 10 月 3 日
sir.,what i do if i want graph as well as input.

サインインしてコメントする。

その他の回答 (2 件)

James Johnson
James Johnson 2019 年 12 月 19 日
編集済み: James Johnson 2019 年 12 月 21 日
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 "&nbsp" 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,' ','&nbsp');
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 件のコメント
Image Analyst
Image Analyst 2019 年 12 月 21 日
James, I 100% agree with Stephen. You can go ahead and "update my answer with corrections such as yours."
Micheal Omojola
Micheal Omojola 2020 年 12 月 22 日
This is awesome! Thanks James!

サインインしてコメントする。


Paresh Lalwani
Paresh Lalwani 2020 年 10 月 3 日
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 件のコメント
Tamas Galli
Tamas Galli 2020 年 12 月 16 日
編集済み: Tamas Galli 2020 年 12 月 16 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by