fprintf for logical statement

212 ビュー (過去 30 日間)
James Connor
James Connor 2015 年 11 月 30 日
編集済み: Sindar 2020 年 1 月 9 日
im trying to write a single code for an fprintf.
Say I have x=1.9712 nh=0.3224 converged=true (or sometimes false)
I have written so far
fprintf('x is %d nh is %d converged is %d./n',x,nh,converged)
which results in, x is 1.971166e+00 nh is 3.223800e-01 converged is 1./nK>>
If converged=true, I would like it to say something like x is 1.971.. nh is 3.223... convergence achieved converged=true
If converged is false I would like it to say something like x is 1.971.. nh is 3.223... convergence not achieved converged=false
The fprintf is being called in a loop I would like each time its called to have it written on a new line in the command window because now it produces on the same line.
Thanks for the help.

採用された回答

Jan
Jan 2015 年 11 月 30 日
編集済み: Jan 2015 年 11 月 30 日
LogicalStr = {'false', 'true'};
% your loop
fprintf('x is %d nh is %d converged is %s.\n', ...
x, nh, LogicalStr{converged + 1});
Note the \n instead of /n
  1 件のコメント
James Connor
James Connor 2015 年 11 月 30 日
Thank you so much

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

その他の回答 (4 件)

Christophe LB
Christophe LB 2018 年 2 月 6 日
編集済み: Christophe LB 2018 年 2 月 6 日
Also a late answer but "mat2str" does the trick easily.
>> fprintf('x is %d nh is %d converged is %s.\n',x,nh,mat2str(converged))
x is 1.971200e+00 nh is 3.224000e-01 converged is true.
  1 件のコメント
Stephen23
Stephen23 2018 年 2 月 6 日
+1 neat and simple

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


Thomas Schmidt
Thomas Schmidt 2017 年 12 月 23 日
I know its a late answer, but maybe someone else is searching for this.
So, mostly i am using 'string (x)' to convert a logical into a string (true / false).
example:
>> a = true
a =
logical
1
>> string (a)
ans =
"True"
  1 件のコメント
Sindar
Sindar 2020 年 1 月 9 日
編集済み: Sindar 2020 年 1 月 9 日
extra tip if your variable might be 0/1 instead of false/true:
string(~~a)

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


Steven Lord
Steven Lord 2015 年 11 月 30 日
I don't believe there's a specifier that makes the logical values print out as their string representations. You will need to define a variable that contains the string representation.
if converged
status = 'true';
else
status = 'false';
end
and print that string using %s instead of the last %d. As for the newline, change /n to \n in your call.

Pedro Busc
Pedro Busc 2017 年 5 月 26 日
This is so LAME, matlab should convert it internally.
  1 件のコメント
Jan
Jan 2017 年 5 月 26 日
編集済み: Jan 2017 年 5 月 26 日
Sorry, what? What is lame? Do you have a runtime issue with this method? Do you want fprintf to display 'true', when a logical value is provided? But this is not the way the fprintf library works. If is based on the same base functions as the C version.
The suggest methods have the benefit, that they can show 'true', 'True', 'TRUE', 'On', 'Yes', 'Enabled' or what ever.

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by