unicode characters in .m file

Matlab claims to be Unicode compliant, but only characters whose code # is ≤255 can be saved in .m files. I can accept that Matlab code words must be in ASCII, but there should be no such constraints on characters in comments or text strings. It would be nice if useful characters, like "≈", "Ω", "≤","≥", etc., could be saved in comments. On a similar note, while a character whose code is ≥128 and ≤255 can be stored in .m files, it is not possible to include them directly in graphics labels or text; typing '\circ' (1970s LaTex convention) seems clunky compared to "Alt+0176" (or pasted from another document) for the degrees symbol(°).

2 comentarios

Paul McKenzie
Paul McKenzie el 19 de Jul. de 2017
While there now seem to be solutions for storing Unicode characters in .m files, I would still like a better alternative to the clunky LaTex method for special characters on plots. With the advent of Unicode, I don't see why labels, titles, and texts shouldn't be specified directly in Unicode. That would make the .m file text look like the text on the plot. (I think they call that approach 'WYSIWYG'.) Adding an 'Insert Symbol' feature to the editor would make labels, titles, and texts so much more convenient then remembering arcane \<symbol> sequences.
Walter Roberson
Walter Roberson el 19 de Jul. de 2017
unicode characters can be included directly for text() objects when using Interpreter Tex (the default) or Interpreter None . However, at this time unicode characters cannot be included directly for latex.

Iniciar sesión para comentar.

 Respuesta aceptada

Rahul Arora
Rahul Arora el 17 de Jul. de 2017
Editada: Rahul Arora el 17 de Jul. de 2017

7 votos

For R2017a, the steps are similar to the ones described in https://www.mathworks.com/matlabcentral/answers/280988-how-do-i-get-my-matlab-editor-to-read-utf-8-characters-utf-8-characters-in-blank-squares-in-editors. The only difference is that the matlabroot/bin/lcdata.xml file no longer describes all standard locales and encodings. But you can still use this file for customizing the locale database.
For my case, the encoding is windows-1252 (you can find your encoding using the feature('locale') command), and therefore, I added the following lines to the <lcdata> ... </lcdata> block in lcdata.xml.
<codeset>
<encoding name="UTF-8">
<encoding_alias name="windows-1252" />
</encoding>
</codeset>
Just modify based on your own system encoding.

4 comentarios

Paul McKenzie
Paul McKenzie el 18 de Jul. de 2017
OK, this fix seems to work. Why isn't this version of lcdata.xml the Mathworks default? I can't see any downside, except for those (Luddites) who actually want Windows-1252 (ANSI) instead of Unicode
Tom DeLonge
Tom DeLonge el 16 de Ag. de 2017
Editada: Tom DeLonge el 16 de Ag. de 2017
being a bit late here: can I change this property for each user? Unfortunately I do not have access to lcdata.xml, I am not admin on my machine... Also lcdata.xml does not have any
<codeset>..</codeset>
entries on my machine, just a few commented lines
Andrey Kazak
Andrey Kazak el 13 de Mayo de 2019
The fix does not work on 2018b for Windows.
Please suggest a workaround.
Thank you.
Rahul Arora
Rahul Arora el 28 de Nov. de 2019
Hi Andrey
I just tried doing this on my MATLAB installation (R2019b, Windows 10) and it works fine. Please note that you have to restart MATLAB after performing the edit I suggested.

Iniciar sesión para comentar.

Más respuestas (5)

MathWorks Support Team
MathWorks Support Team el 19 de Feb. de 2021
Editada: MathWorks Support Team el 19 de Feb. de 2021

2 votos

As of R2020a, the MATLAB Editor supports UTF-8 characters, and uses UTF-8 as the default encoding for new plain text files, including MATLAB code files with a .m extension.
Jan
Jan el 19 de Mayo de 2017

0 votos

14 comentarios

Paul McKenzie
Paul McKenzie el 23 de Mayo de 2017
Answer does not seem applicable to R2017a.
Paul McKenzie
Paul McKenzie el 23 de Mayo de 2017
The answer from Jinghao Lei ('UTF- encoding')seems to have been successful on some earlier (Chinese?) version of Matlab, but again seems not applicable to R2017a.
Yueyan Li
Yueyan Li el 13 de En. de 2018
Editada: Yueyan Li el 13 de En. de 2018
The answer from Jinghao Lei https://cn.mathworks.com/matlabcentral/answers/280988-how-do-i-get-my-matlab-editor-to-read-utf-8-characters-utf-8-characters-in-blank-squares-in-editors does work on MATLAB R2017a, just rename the lcdata_utf8.xml to lcdata.xml first.
Paul McKenzie
Paul McKenzie el 15 de En. de 2018
An elegantly simple solution! The file names even make sense now. I wish that Mathworks had provided an option during installation to make this file name change, since many corporate users aren't allowed to make changes inside the "Program Files" directory.
Paul McKenzie
Paul McKenzie el 10 de Mayo de 2018
What happened? This solution was working a week ago; today, unicode characters are displaying as garbage. Did some Windows update foul Matlab up? Why is only Matlab susceptible?
Walter Roberson
Walter Roberson el 10 de Mayo de 2018
Paul McKenzie: did you happen to do Windows Creator Spring Update within the last week?
Paul McKenzie
Paul McKenzie el 11 de Mayo de 2018
Editada: Paul McKenzie el 11 de Mayo de 2018
I don't think so; I'm running Win 7. I've seen this problem on two Win 7 machines.
Duijnhouwer
Duijnhouwer el 31 de Ag. de 2018
Editada: Walter Roberson el 31 de Ag. de 2018
Paul McKenzie
Paul McKenzie el 12 de Sept. de 2018
After multiple conversations with tech support, it is clear that the editor design team did not intend to make it Unicode compatible. R2018b will not cure the problem. Perhaps some future release will be truly Unicode compatible, but in the interim, Notepad++ seems the only alternative that supports Unicode.
Walter Roberson
Walter Roberson el 12 de Sept. de 2018
Mathworks needs to do more to make it easy to use Unicode in source code, UTF-8 at least.
Paul McKenzie
Paul McKenzie el 30 de Abr. de 2019
Editada: Paul McKenzie el 1 de Mayo de 2019
R2019a is out, but the editor still cannot handle Unicode!
Note to Mathworks Editor Design Team: Check out the comprehensive and elegant way Octave allows one to select text encoding.
Paul McKenzie
Paul McKenzie el 14 de Sept. de 2019
Now R2019b is out, and the editor still cannot handle unicode. Since that's the case, why does Matlab still include the file "lcdata_utf8.xml", since it no longer provides any unicode capability?
Paul McKenzie
Paul McKenzie el 3 de Abr. de 2020
R2020a is out and solves the problem! All .m files are saved in UTF-8 format.
Shaul Shvimmer
Shaul Shvimmer el 31 de Jul. de 2020
I'm using MATLAB 2020a and I still have the problem - I cannot change encoding to UTF-8 using Windows 10.

Iniciar sesión para comentar.

Muhammad  Kashif
Muhammad Kashif el 14 de Mzo. de 2018
Editada: Muhammad Kashif el 6 de Abr. de 2018

0 votos

Assalam O Alikum and good day all
For those using languages having Arabic script e.g. Arabic ,Urdu.
1)Change the system Locale to Urdu(Pakistan) for Urdu.For Arabic, change the system locale accordingly.
2)For displaying Urdu and Arabic in Matlab Command prompt,use the following command:
>>slCharacterEncoding('UTF-8')
3) In the matlab editor, to make the string functions like strsplit() work properly for Urdu and Arabic, this should be the first line of code:
feature('DefaultCharacterSet', 'UTF8');
Best regards

1 comentario

Walter Roberson
Walter Roberson el 14 de Sept. de 2019
Unfortunately, slCharacterEncoding() is for Simulink use, not the MATLAB command prompt.

Iniciar sesión para comentar.

Jean-Michel Rousseau
Jean-Michel Rousseau el 10 de En. de 2020

0 votos

Hello,
Is ther any way to definitively change the 'DefaultCharacterSet' without the need to use the feature function at every start of Matlab?
I need to use 'windows-1252' and Matlab (& Simulink) always start with "UTF-8". It as recently canged after updating to the R2019b. As I can't solve the problem into R2019b (my folders and files with accent can't be used anymore without renaming them), i go back to a fresh R2016b but the problem remain :-(

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Preguntada:

el 18 de Mayo de 2017

Editada:

el 19 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by