Live editor formatted text output

조회 수: 82 (최근 30일)
greengrass62
greengrass62 2018년 2월 6일
댓글: Frank Schumann 2023년 2월 25일
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:
  댓글 수: 1
Christopher Coello
Christopher Coello 2018년 2월 7일
Yes something I wondered as well.

댓글을 달려면 로그인하십시오.

채택된 답변

Christopher Coello
Christopher Coello 2018년 2월 7일
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
Christopher Coello 2018년 2월 7일
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
Christopher Coello 2018년 2월 7일
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
  댓글 수: 1
greengrass62
greengrass62 2020년 11월 10일
Thank you for the alternatives. gg62

댓글을 달려면 로그인하십시오.


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