fprintf Multiple Lines?

211 visualizzazioni (ultimi 30 giorni)
Stephen
Stephen il 24 Apr 2012
Commentato: Stephen23 il 5 Lug 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!

Risposta accettata

Walter Roberson
Walter Roberson il 24 Apr 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 Commenti
M. Mubashir Hussain
M. Mubashir Hussain il 5 Lug 2023
Doesn't work with sprintf command. Possible solution for multiline text using sprintf()?
Stephen23
Stephen23 il 5 Lug 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)
str =
'The results of test 99 are such that 99 of the cats are older than 99 years old. The results of test 99 are such that 99 of the dogs are older than 99 years old. The results of test 99 are such that 99 of the fish are older than 99 years old. '

Accedi per commentare.

Più risposte (2)

Image Analyst
Image Analyst il 24 Apr 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 Commenti
Sanjib Lamichhane
Sanjib Lamichhane il 7 Mag 2019
Thanks a lot!
Raymond MacNeil
Raymond MacNeil il 15 Giu 2019
Modificato: Raymond MacNeil il 15 Giu 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!

Accedi per commentare.


Dan Po
Dan Po il 27 Ott 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 Commenti
Daniel Espinoza
Daniel Espinoza il 3 Set 2019
Can you explain what diference does the brackets make? (I am new in matlab)
Walter Roberson
Walter Roberson il 3 Set 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.

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by