0

How to write special character "ā" on a plot using MATLAB text() function?

ASE
  • 1,702
  • 2
  • 21
  • 29
  • What is wrong with `text(0,0,'ā')`? It seems to work for me. – Cris Luengo Mar 09 '20 at 22:02
  • @CrisLuengo `ā` will disappear once you save your m-file. It follows a different encoding. This can be done though: `double('ā')`, save the result (`[196 129]`) and then use that with `char`. i.e. `char([196 129])` – Sardar Usama Mar 09 '20 at 22:06
  • @SardarUsama: It doesn't disappear in an M-file I created and saved on https://matlab.mathworks.com/. – Cris Luengo Mar 09 '20 at 22:09
  • @CrisLuengo does it stay as is if you close the file and reopen it? – Sardar Usama Mar 09 '20 at 22:10
  • @SardarUsama Yes, it does. Maybe it depends on which OS you're on? Or some encoding configuration? – Cris Luengo Mar 09 '20 at 22:10
  • @CrisLuengo For me, `'ā'` changes to `'?'` if I close and reopen the m-file (ANSI Encoding, Win10, MATLAB R2018a, Desktop version) – Sardar Usama Mar 09 '20 at 22:14
  • @SardarUsama You probably need to use some form of Unicode encoding. MATLAB uses UTF-16 internally (or is it UCS-2?) – Cris Luengo Mar 09 '20 at 22:22
  • @CrisLuengo I found the solution [*here*](http://stackoverflow.com/q/4984532). I followed [this answer](https://stackoverflow.com/a/49801069/5698672) to fix it. Thanks anyway – Sardar Usama Mar 10 '20 at 10:23

1 Answers1

1

You can use LaTeX interpretor for achieving this. Here is an example:

p = plot(1,2,'O');
hold on
pCS = plot(1,3,'rO');
txt = text(1,1,{'p';'$\bar{a}$'});
set(txt,'Interpreter','latex');
ASE
  • 1,702
  • 2
  • 21
  • 29
Prashant
  • 4,775
  • 3
  • 28
  • 47
  • This will plot "â". I need to plot "ā" – ASE Mar 09 '20 at 22:05
  • 1
    The correct one is '$\bar{a}$'. Please change your answer accordingly. txt=text(1,1,{'p$\bar{a}$'}); set(txt,'Interpreter','latex'); – ASE Mar 09 '20 at 22:07