print non-ASCII / symbolic characters in Matlab

2019-06-25 09:03发布

问题:

I'd like to have a statement that did:

my_angle = 1*pi;
fprintf('My angle is %.3f pi.\n',my_angle/pi);

but that produced My angle is 1.000 pi, instead of the actual π character.

I'm thinking some sort of use of Unicode...

I found some related things:

  • Page on Unicode pi
  • Sort of related SO question
  • Perhaps a more relevant SO question

回答1:

I don't know how to do it with fprintf, but sprintf works – just leave off the semicolon:

sprintf('My angle is %.3f %c.\n',my_angle,char(960))

Or you can use disp:

disp(['My angle is ' num2str(my_angle,'%.3f') ' ' char(960) '.']);


回答2:

The answer depends on your OS. On Windows Matlab uses the windows-1252 character set which is pretty limited. I think char with values greater than 255 you get nothing/squares. On Linux you can use full UTF8 character sets and can use char with any value you want.