Fprintf not inserting new line correctly with /n

12 ビュー (過去 30 日間)
BC
BC 2021 年 3 月 31 日
コメント済み: BC 2021 年 3 月 31 日
I have a loop and part of it includes displaying text in the command window only, I don't need to export the string.
I've tried different positions of using /n in fprintf but I can't seem to get it to work properly. After the whole text string, I need a line break. I've also tried \r and \n together, but I get the same results. I assume I'm missing something obvious. I'm not quite sure where the /n is supposed to go.
Using disp and sprintf works as intended, as an example of what I want it do to.
% assign variable
InputFolders = [1 1 1];
% disp and sprintf works as intended
disp(sprintf("%s","Length of input and output folders match (",num2str(length(InputFolders)),")"))
disp(sprintf("%s","Length of input and output folders match (",num2str(length(InputFolders)),")"))
%%
% No \n included
fprintf("%s","Length of input and output folders match (",num2str(length(InputFolders)),")")
fprintf("%s","Length of input and output folders match (",num2str(length(InputFolders)),")")
%%
% \n on end - the \n is printed, presumably because I have %s at the start reading it as text
fprintf("%s","Length of input and output folders match (",num2str(length(InputFolders)),")\n")
fprintf("%s","Length of input and output folders match (",num2str(length(InputFolders)),")\n")
%%
% \n at start - gives a new line, but after every part of text, not at the end of the string
fprintf("%s\n","Length of input and output folders match (",num2str(length(InputFolders)),")")
fprintf("%s\n","Length of input and output folders match (",num2str(length(InputFolders)),")")

採用された回答

per isakson
per isakson 2021 年 3 月 31 日
編集済み: per isakson 2021 年 3 月 31 日
The documentation of fprinf says
fprintf(formatSpec,A1,...,An) applies the formatSpec to all elements of arrays A1,...An in column order, and displays the results on the screen.
For every A1,A2,...,An the formatSpec should (typically) contain one Formatting Operator (or the formatSpec is reused).
Example
fprintf("%s\n","Length of input and output folders match (", "3" ,")")
The formatting operator, "%s\n", is reused three times: for "Length of input and output folders match (", for "3" and for ")" You intended (I guess)
fprintf( "Length of input and output folders match (%d)\n", 3 )
which outputs
Length of input and output folders match (3)
  3 件のコメント
per isakson
per isakson 2021 年 3 月 31 日
編集済み: per isakson 2021 年 3 月 31 日
"Interesting that the disp and sprintf method doesn't work for you" Indeed it does. I firstly replaced (too save on line length) num2str(length(InputFolders)) by 3 (which didn't work). With "3" it works.
BC
BC 2021 年 3 月 31 日
Ok that makes sense - simiarily, with using your fprintf solution, instead of (%d) it should be (%s) if using my original line, as it's a string from num2str, not just a number. Just in case anyone else looks at this post :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by