4

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:

Community
  • 1
  • 1
Frederick
  • 1,271
  • 1
  • 10
  • 29
  • It's not really similar to that SO question unless you're putting text on a plot. Are you writing to a file or just trying to print to the command window? Maybe see [this question and answer](http://stackoverflow.com/questions/12415767/write-unicode-strings-to-a-file-in-matlab). – horchler Nov 07 '13 at 22:05
  • Just trying to print to the command window. I also saw that page while I was searching, but lost it before forming this question. I've added to the question body. – Frederick Nov 07 '13 at 23:07

2 Answers2

4

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) '.']);
horchler
  • 18,384
  • 4
  • 37
  • 73
  • While this should work on Linux, and maybe OSX, I don't think it will work on Windows. – StrongBad Nov 08 '13 at 00:01
  • @DanielE.Shub: Not sure if it's a valid test, but on R2013a and OS X I changed my default character set (`feature('DefaultCharacterSet','Windows-1252')`) and these both still worked. – horchler Nov 08 '13 at 00:07
  • +1 Very helpful and inspiring. Even *within* the extended ASCII range, just think about `char(177)` for ± instead of `+/-`, or `char(215)` for × in your printouts. Thanks. – s.bandara Nov 08 '13 at 00:11
  • @s.bandara: With a bit of Unicode you can do [crazy things in Matlab](http://biorobots.cwru.edu/personnel/adh/stackoverflow/05/diacritics.png). Displaying things in the command window is harder though. – horchler Nov 08 '13 at 00:22
  • Wow, you rendered those into an image matrix, or just `text` into axes? – s.bandara Nov 08 '13 at 00:36
  • Haha, now my only problem is my font (both Ubuntu and OSX MATLAB) doesn't include that character. Hmm.... – Frederick Nov 08 '13 at 00:45
-1

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.

StrongBad
  • 869
  • 6
  • 16