MATLAB Answers

0

fprintf Multiple Lines?

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

로그인 to comment.

답변 수: 3

Answer by Walter Roberson
on 24 Apr 2012
 Accepted Answer

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);

  댓글 수: 2

Stephen on 24 Apr 2012
Works like a dream, thank you.
Thank you Walter, 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!

로그인 to comment.


Answer by Image Analyst
on 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

Stephen on 24 Apr 2012
Thank you very much for explaining how to do it, very much appretiated :)
Thanks a lot!
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!

로그인 to comment.


Answer by Dan Po
on 27 Oct 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

표시 이전 댓글 수: 2
Image Analyst
on 28 Oct 2016
That's a neat trick!
Can you explain what diference does the brackets make? (I am new in matlab)
Walter Roberson
on 3 Sep 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.

로그인 to comment.



Translated by