Live editor formatted text output

64 views (last 30 days)
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:

Accepted Answer

Christopher Coello
Christopher Coello on 7 Feb 2018
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);

More Answers (3)

Christopher Coello
Christopher Coello on 7 Feb 2018
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 Comment
Christopher Coello
Christopher Coello on 7 Feb 2018
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)

Sign in to comment.


K.
K. on 10 Nov 2020
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. on 10 Nov 2020
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 Comments
greengrass62
greengrass62 on 10 Nov 2020
Thank you for the alternatives. gg62
Frank Schumann
Frank Schumann on 25 Feb 2023
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.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!