fprintf Multiple Lines?
213 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Stephen
el 24 de Abr. de 2012
Comentada: Stephen23
el 5 de Jul. de 2023
Is there a way to print multiple lines to the command window (like fprintf does for single lines) without using fprintf at the start of each line?
At the moment my code looks similar (the below was quickly made up as an example) to;
fprintf ('The results of test %d are such that %d ', t1, cats)
fprintf ('of the cats are older than %d years old.\n', age)
fprintf ('The results of test %d are such that %d ', t2, dogs)
fprintf ('of the dogs are older than %d years old.\n', age)
fprintf ('The results of test %d are such that %d ', t3, fish)
fprintf ('of the fish are older than %d years old.\n', age)
Is there a way to only state fprintf once at the start then have the text followed by the variables at the end? Maybe a bit like;
fprintf ('The results of test %d are such that %d of the ...
cats are older than %d years old.\nThe results of test ...
%d are such that %d of the dogs are older than %d years ...
old.\nThe results of test %d are such that %d of the ...
fish are older than %d years old.\n', t1, cats, age, ...
t2, dogs, age, t3, fish, age)
I've searched the help files and looked into sprintf but I couldn't see anything that does this so thought I'd see if I'm wasting my time searching!
The help is much appreciated, and apologise if I've missed something staring me in the face in the help files!
0 comentarios
Respuesta aceptada
Walter Roberson
el 24 de Abr. de 2012
Yes, that second syntax for fprintf() itself is fine. You are being caught on the fact that string constants cannot be continued by using ...
fprintf( ['The results of test %d are such that %d of the ', ...
'cats are older than %d years old.\nThe results of test ', ...
'%d are such that %d of the dogs are older than %d years ', ...
'old.\nThe results of test %d are such that %d of the ', ...
'fish are older than %d years old.\n'], t1, cats, age, ...
t2, dogs, age, t3, fish, age);
4 comentarios
M. Mubashir Hussain
el 5 de Jul. de 2023
Doesn't work with sprintf command. Possible solution for multiline text using sprintf()?
Stephen23
el 5 de Jul. de 2023
"Doesn't work with sprintf command."
It does work with SPRINTF command. Lets try it right now:
[t1,cats,age,t2,dogs,age,t3,fish,age] = deal(99);
str = sprintf( ['The results of test %d are such that %d of the ', ...
'cats are older than %d years old.\nThe results of test ', ...
'%d are such that %d of the dogs are older than %d years ', ...
'old.\nThe results of test %d are such that %d of the ', ...
'fish are older than %d years old.\n'], t1, cats, age, ...
t2, dogs, age, t3, fish, age)
Más respuestas (2)
Image Analyst
el 24 de Abr. de 2012
You can do it with one line but you can't split up the fprintf inside the quotes, you have to at least wait until the closing quote
fprintf('blah blah blah %d snafu fubar %s lorem ipsum',... % Format specifier string MUST be on one line.
myInteger, myString); % List of variables can be on the next line.
3 comentarios
Raymond MacNeil
el 15 de Jun. de 2019
Editada: Raymond MacNeil
el 15 de Jun. de 2019
Thank you Image Analyst, as always. Always great that these minor syntactical hurdles that relatively new Matlab users confront can easily be found and answered on the MATLAB Answers forum. I just want to say thanks for the time you've put into to answering questions for the community over the years...it is really appreciated, and I couldn't count the number of posts where it's the usual players providing succint and clear answers. Dating back to 2012 when the question was intially asked and still relevant...thanks again!
Dan Po
el 27 de Oct. de 2016
it doesnt seem to be working for me.
Im getting repeated fprintf's when i enter:
y1=3
fprintf('filler fillerfiller filler %.2f:filler fillerfiller filler',...
'filler fillerfiller fillerfiller fillerfiller filler \n',y1)
would anyone know the cause? here's the output:
filler fillerfiller filler 102.00:filler fillerfiller fillerfiller fillerfiller filler 105.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 101.00:filler fillerfiller fillerfiller fillerfiller filler 114.00:filler fillerfiller fillerfiller fillerfiller filler 32.00:filler fillerfiller fillerfiller fillerfiller filler 102.00:filler fillerfiller fillerfiller fillerfiller filler 105.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 101.00:filler fillerfiller fillerfiller fillerfiller filler 114.00:filler fillerfiller fillerfiller fillerfiller filler 102.00:filler fillerfiller fillerfiller fillerfiller filler 105.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 101.00:filler fillerfiller fillerfiller fillerfiller filler 114.00:filler fillerfiller fillerfiller fillerfiller filler 32.00:filler fillerfiller fillerfiller fillerfiller filler 102.00:filler fillerfiller fillerfiller fillerfiller filler 105.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 101.00:filler fillerfiller fillerfiller fillerfiller filler 114.00:filler fillerfiller fillerfiller fillerfiller filler 102.00:filler fillerfiller fillerfiller fillerfiller filler 105.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 101.00:filler fillerfiller fillerfiller fillerfiller filler 114.00:filler fillerfiller fillerfiller fillerfiller filler 32.00:filler fillerfiller fillerfiller fillerfiller filler 102.00:filler fillerfiller fillerfiller fillerfiller filler 105.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 101.00:filler fillerfiller fillerfiller fillerfiller filler 114.00:filler fillerfiller fillerfiller fillerfiller filler 102.00:filler fillerfiller fillerfiller fillerfiller filler 105.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 101.00:filler fillerfiller fillerfiller fillerfiller filler 114.00:filler fillerfiller fillerfiller fillerfiller filler 32.00:filler fillerfiller fillerfiller fillerfiller filler 102.00:filler fillerfiller fillerfiller fillerfiller filler 105.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 108.00:filler fillerfiller fillerfiller fillerfiller filler 101.00:filler fillerfiller fillerfiller fillerfiller filler 114.00:filler fillerfiller fillerfiller fillerfiller filler 32.00:filler fillerfiller fillerfiller fillerfiller filler 92.00:filler fillerfiller fillerfiller fillerfiller filler 110.00:filler fillerfiller fillerfiller fillerfiller filler 3.00:filler fillerfiller filler>> y1
5 comentarios
Daniel Espinoza
el 3 de Sept. de 2019
Can you explain what diference does the brackets make? (I am new in matlab)
Walter Roberson
el 3 de Sept. de 2019
[] is the matrix building operation. The syntax [A, B] is equivalent to horzcat(A, B) which is in turn the same as cat(2, A, B), which is to say that it puts rows beside each other to create a new matrix with more columns. The syntax
fprintf( ['filler fillerfiller filler %.2f:filler fillerfiller filler',...
'filler fillerfiller fillerfiller fillerfiller filler \n'], y1)
is equivalent to
temporary_variable = ['filler fillerfiller filler %.2f:filler fillerfiller filler',...
'filler fillerfiller fillerfiller fillerfiller filler \n'];
fprintf(temporary_variable, y1)
which is passing two arguments to fprintf, the first of which will be used as the format to use.
However, the syntax
fprintf('filler fillerfiller filler %.2f:filler fillerfiller filler',...
'filler fillerfiller fillerfiller fillerfiller filler \n',y1)
would be passing three arguments to fprintf, the first being used as the format, and the second one 'filler fillerfiller fillerfiller fillerfiller filler \n' as data to be printed out using the format from the first parameter.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!