Live editor formatted text output
68 views (last 30 days)
Show older comments
greengrass62
on 6 Feb 2018
Commented: Frank Schumann
on 25 Feb 2023
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
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);
0 Comments
More Answers (3)
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
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)
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
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
2 Comments
Frank Schumann
on 25 Feb 2023
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.
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!