fprintf Multiple Lines?
조회 수: 210 (최근 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!