how to set the encoding of MATLAB m-files to be UTF8

313 views (last 30 days)
I want to insert an m-file into LaTeX, and there are Chinese characters in my m-files, so I need the encoding of m-files to be UTF8.I have searched for some materials on the web, and I found the MATLAB command: slCharacterEncoding. So, I closed all the m-files and input the" slCharacterEncoding('UTF-8') "in the command window. But when I opened the m-file using Notepad++, the encoding with Chinese characters still is GB2312, and the m-files without Chinese characters is normal (UTF8).
how should I do to set the matlab editor to be UTF8 encoding? thanks

Answers (2)

Image Analyst
Image Analyst on 27 Sep 2019
Maybe open the file for reading, then open a file for writing using fopen(filename, , wt, 'UTF-8') (not sure if there is a parameter name that goes before UTF-8 so check the documentation. Then read from one file and write to the output file.
  1 Comment
William
William on 27 Sep 2019
I have input the command to set the matlab editor to be UTF8 .But when I open the main.m file using notepad++, the encoding of main.m is still GB2312, because there are Chinese characters in the main.m file. follows:as follows, what should I do? thanks.
QQ截图20190927160053.png
QQ截图20190927160135.png

Sign in to comment.


MathWorks Support Team
MathWorks Support Team on 19 Feb 2021
Edited: MathWorks Support Team on 19 Feb 2021
As of R2020a, the MATLAB Editor will default to saving new files using UTF-8. When opening existing files, the Editor and other functions like type or fopen automatically determine the current encoding. One can explicitly specify that the file should be saved using UTF-8 using the Save As dialog.

Community Treasure Hunt

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

Start Hunting!