How do I use Greek letters in fprintf ? Since fprintf uses \ in its argument, I cant write Greek letters in it. Thanks

628 ビュー (過去 30 日間)
ebrahimina
ebrahimina 2017 年 7 月 1 日
コメント済み: DGM 2023 年 8 月 5 日
fprintf(' \lambda %g\t',10)
Warning: Escape sequence 'l' is not valid.See 'help sprint' for valid escape sequences.

回答 (4 件)

Arpan Bhowmik
Arpan Bhowmik 2018 年 3 月 30 日
Try escaping the escape character:
fprintf('\\lambda %g\t',10)
I know this works to get Greek characters in sprintf.
  5 件のコメント
Syed Ali
Syed Ali 2022 年 5 月 9 日
Thanks Arpan, solved my problem

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


Walter Roberson
Walter Roberson 2017 年 7 月 1 日
fprintf('\x03bb %g\t', 10)
  11 件のコメント
Jerry Malone
Jerry Malone 2023 年 8 月 4 日
移動済み: Voss 2023 年 8 月 4 日
Just curious, why does this work?
>> fprintf('(RLS) \x03bb: [%4.6E, %4.6E, %4.6E]\n',lambda);
(RLS) λ: [9.999000E-01, 9.999800E-01, 9.999000E-01]
But this does not?
>> disp(sprintf('(RLS) \\lambda: [%4.6E, %4.6E, %4.6E]\n',lambda))
(RLS) \lambda: [9.999000E-01, 9.999800E-01, 9.999000E-01]
Nor does this:
>> disp(sprintf('(RLS) \lambda: [%4.6E, %4.6E, %4.6E]\n',lambda))
Warning: Escaped character '\l' is not valid. See 'doc sprintf' for supported special
characters.
(RLS)
Walter Roberson
Walter Roberson 2023 年 8 月 5 日
The character vector \lambda is coding for TeX or LaTeX, and is completely unknown to sprintf() or fprintf() or compose() or to the MATLAB command window generally.
\x03bb is using the documented fprintf() / sprintf() facility to encode character positions in the current font (which is not necessarily Unicode)
You might also have seen something like
syms lambda
'(RLS)', disp(lambda), '[9.999000E-01, 9.999800E-01, 9.999000E-01]'
ans = '(RLS)'
λ
ans = '[9.999000E-01, 9.999800E-01, 9.999000E-01]'
This only works with the Symbolic Toolbox when it is being displayed in LiveScript mode or being displayed in MATLAB Online or MATLAB Answers. And it is difficult to insert into the middle of a string.

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


Image Analyst
Image Analyst 2017 年 7 月 3 日
I can get plenty of Greek symbols in the command window doing this:
>> str = sprintf('%c ', 900:1000)
Just pick the right number.
  4 件のコメント
Image Analyst
Image Analyst 2020 年 4 月 15 日
I'd call the Mathworks on that, because for me in R2020a, I do see lots of Greek symbols:
If you don't see what I do above, then maybe there is some kind of regionalization of the operating system that is not allowing you to see them. The Mathworks should be able to talk you through it. Come back here and let us know how they fixed it for you.
DGM
DGM 2023 年 8 月 5 日
Works for me in R2009b. Maybe that's more of a system problem than the version?

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


Spencer Cochran
Spencer Cochran 2019 年 11 月 20 日
編集済み: Spencer Cochran 2019 年 11 月 20 日
Leverage the Unicode representation of the Greek Letter.
'lambda' in Unicode is the Hex number: '039B' (You can find Unicode lists online).
Convert from the Hex Unicode character string -> to decimal -> to char -> concatenate with other character strings.
Lambda = char(hex2dec('039B'));
Omega = char(937);
disp(['disp Function: ' Lambda ' | ' Omega]);
fprintf(['fprintf Function: ' Lambda ' | ' Omega '\n']);
This code gives:
disp Function: Λ | Ω
fprintf Function: Λ | Ω
There may be more elegant ways of doing this, but it works for me!
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 11 月 22 日
This is the same basic approach as I suggested above where I suggested
fprintf('\x03bb %g\t', 10)
The \x03bb codes for U+03BB which is λ (lower-case Lambda).
The difficulty with this turned out to be that the person asking the question was using an older version of Windows that did not have the right font or code page activated. It took Microsoft a long time to catch up on Unicode (if they even have caught up yet.)

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

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by