fprintf Multiple Lines?
209 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2012-4-24
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 个评论
M. Mubashir Hussain
2023-7-5
Doesn't work with sprintf command. Possible solution for multiline text using sprintf()?
Stephen23
2023-7-5
"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)
更多回答(2 个)
Image Analyst
2012-4-24
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 个评论
Raymond MacNeil
2019-6-15
编辑:Raymond MacNeil
2019-6-15
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
2016-10-27
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 个评论
Daniel Espinoza
2019-9-3
Can you explain what diference does the brackets make? (I am new in matlab)
Walter Roberson
2019-9-3
[] 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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!