Live editor formatted text output
68 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
greengrass62
el 6 de Feb. de 2018
Comentada: Frank Schumann
el 25 de Feb. de 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:
1 comentario
Respuesta aceptada
Christopher Coello
el 7 de Feb. de 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 comentarios
Más respuestas (3)
Christopher Coello
el 7 de Feb. de 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 comentario
Christopher Coello
el 7 de Feb. de 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.
el 10 de Nov. de 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.
el 10 de Nov. de 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 comentarios
Frank Schumann
el 25 de Feb. de 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.
Ver también
Categorías
Más información sobre Environment and Settings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!