Live editor formatted text output

77 次查看(过去 30 天)
How do I convert fprintf statements (from *.m) to live editor if I am interested in maintaining tab alignment and line feeds and line breaks? For example, the below line of code looks ok in *.m, but doesn't look too helpful when outputted to pdf because of the alternating lines of code and output.
company='Acme'; year=2015; volume=14.56; cost=1564;
fprintf('%s Report\n\n',company)
fprintf('Year\tVolume\tCost\n')
fprintf('%4.0f\t%0.2f\t%0.2f\n',year,volume,cost)
command window output:
Acme Report
Year Volume Cost
2015 14.56 1564.00|
Live editor to pdf looks like:

采纳的回答

Christopher Coello
Or just simply one fprintf
company='Acme'; year=2015; volume=14.56; cost=1564;
fprintf(['%s Report\n\n',...
'Year\tVolume\tCost\n',...
'%4.0f\t%0.2f\t%0.2f\n'],...
company,year,volume,cost);

更多回答(3 个)

Christopher Coello
Possible solution : write everything in a string using sprintf and then use disp to display it when the string is finished formatted.
ct=sprintf('%s\n| Out of bag sample -- only non night timepoints\n| mae %0.0f MWh/h\n%s\n',repmat('-',35),mae,repmat('-',35));
disp(ct)
  1 个评论
Christopher Coello
Applied to your example
company='Acme'; year=2015; volume=14.56; cost=1564;
fstr = sprintf(['%s Report\n\n',...
'Year\tVolume\tCost\n',...
'%4.0f\t%0.2f\t%0.2f\n'],...
company,year,volume,cost);
disp(fstr)

请先登录,再进行评论。


K.
K. 2020-11-10
I know this is an old thread, but in case others come across this, I'd like to provide a workaround in addition to the two solutions provided by Christopher.
If it is possible, putting your print code into a local function would result in the texual output appearing together. E.g.
company='Acme'; year=2015; volume=14.56; cost=1564;
showReport(company, year, volume, cost);
function showReport(company, year, volume, cost)
fprintf('%s Report\n\n',company)
fprintf('Year\tVolume\tCost\n')
fprintf('%4.0f\t%0.2f\t%0.2f\n',year,volume,cost)
end
Example showing local function

K.
K. 2020-11-10
Another solution might be to wrap the code in an if statement, as that would cause the output to be grouped. It doesn't look very nice, but if you have some other reason to put your code in an if statement, there might be times where this could be a workaround.
company='Acme'; year=2015; volume=14.56; cost=1564;
showReport = true;
if showReport
fprintf('%s Report\n\n',company)
fprintf('Year\tVolume\tCost\n')
fprintf('%4.0f\t%0.2f\t%0.2f\n',year,volume,cost)
end
Example showing if statement
  2 个评论
greengrass62
greengrass62 2020-11-10
Thank you for the alternatives. gg62
Frank Schumann
Frank Schumann 2023-2-25
Thank you @greengrass62.
Will Mathworks provide a more elgant solution ? This seems like how the Live Editor treats and displays command line output is misconceived. This should be easier. For example, simply always write under the current code block.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by