MATLAB 源文件中的 Unicode 字符

发布于 2024-10-17 13:51:28 字数 312 浏览 2 评论 0原文

我想在 注释中使用 Unicode 字符/wiki/MATLAB">MATLAB 源文件。当我写文字时,这似乎有效;但是,如果我关闭文件并重新加载它,“不寻常”的字符就会变成问号。我猜 MATLAB 正在将文件保存为 ASCII。

有没有办法告诉 MATLAB 使用 UTF-8 代替?

I'd like to use Unicode characters in comments in a MATLAB source file. This seems to work when I write the text; however, if I close the file and reload it, "unusual" characters have been turned into question marks. I guess MATLAB is saving the file as ASCII.

Is there any way to tell MATLAB to use UTF-8 instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

汹涌人海 2024-10-24 13:51:28

根据 http://www.mathworks.de/matlabcentral/newsreader/view_thread/238995

feature('DefaultCharacterSet', 'UTF8')

将更改编码为 UTF-8。您可以将上面的行放入您的startup.m 文件中。

According to http://www.mathworks.de/matlabcentral/newsreader/view_thread/238995

feature('DefaultCharacterSet', 'UTF8')

will change the encoding to UTF-8. You can put the line above in your startup.m file.

献世佛 2024-10-24 13:51:28

MATLAB 进程如何使用区域设置展示了如何设置不同平台的编码。使用

feature('DefaultCharacterSet')

您可以在此处阅读有关此未记录函数的更多信息。另请参阅此 Matlab Central 线程 用于其他选项。

How the MATLAB Process Uses Locale Settings shows how to set the encoding for different platforms. Use

feature('DefaultCharacterSet')

You can read more about this undocumented function here. See also this Matlab Central thread for other options.

末蓝 2024-10-24 13:51:28

仅限 Mac OSX!

当我找到适合我的情况的解决方案时,我想分享它。

Mathworks 建议此处使用slCharacterEncoding(encoding) 以便根据需要更改编码,但对于 OSX,这并不能完全解决问题,因为接受的答案中的 feature('DefaultCharacterSet') 没有这样做。 MATLAB 答案中的以下链接帮助我获取用于打开和保存 .m 文件的 UTF-8 编码集:
https://www.mathworks.com/matlabcentral/answers/12422- macosx-encoding-problem

Matlab 似乎忽略 slCharacterEncoding(encoding)feature('DefaultCharacterSet') 中设置的任何值,但使用系统偏好设置中设置的区域 - >语言与地区。检查在我们的例子中选择了哪个区域后,就可以在

 $matlabroot/bin/lcdata.xml

这个目录中的隐藏配置文件中定义实际编码,可以通过进入应用程序来打开此目录,然后右键单击Matlab,选择显示包内容,如屏幕截图所示(此处)德语)

“包内容”

例如,对于德国默认 ISO-8859-1,可以通过将文件 lcdata.xml 中的相应行更改

 <locale name="de_DE" encoding="ISO-8859-1" xpg_name="de_DE.ISO8859-1">

 <locale name="de_DE" encoding="UTF-8" xpg_name="de_DE.UTF-8">

:如果所选区域不存在于lcdata.xml 文件这将不起作用。

希望这有帮助!

Mac OSX only!

As I found solution which worked in my case I want to share it.

Mathworks advises here to use slCharacterEncoding(encoding) in order to change the encoding as desired, but for the OSX this does not solve the issue exactly as the feature('DefaultCharacterSet') in accepted answer does not do it. What helped me to get the UTF-8 encoding set for opening and saving .m files was the following link on MATLAB answers:
https://www.mathworks.com/matlabcentral/answers/12422-macosx-encoding-problem

Matlab seems to ignore any value set in slCharacterEncoding(encoding) or feature('DefaultCharacterSet') but uses region set in System Preferences -> Language & Region. After checking which region is selected in our case then it is possible to define the actual encoding in the hidden configuration file in

 $matlabroot/bin/lcdata.xml

This directory can be opened by getting to the Applications and after right click on Matlab by selecting Show Package Contents as on screenshot (here in German)

Package Contents

For example for German default ISO-8859-1 it is possible to adjust it by changing the respective line in the file lcdata.xml:

 <locale name="de_DE" encoding="ISO-8859-1" xpg_name="de_DE.ISO8859-1">

to:

 <locale name="de_DE" encoding="UTF-8" xpg_name="de_DE.UTF-8">

If the region which is selected is not present in the lcdata.xml file this will not work.

Hope this helps!

一向肩并 2024-10-24 13:51:28

此处提供的解决方案适用于我在 Windows R2018a 上。

如果链接不起作用:想法是使用文件 matlabroot/bin/lcdata.xml 来配置编码名称的别名(可以在评论中的这个文件中找到一些解释):

<codeset>
  <encoding name="UTF-8">
   <encoding_alias name="windows-1252" />
  </encoding>
</codeset>

您可以使用自己的值而不是 windows-1252,当前使用的编码可以通过运行 feature('locale') 获取。

但是,如果您在帮助注释中使用 Unicode 字符,帮助浏览器将无法识别它们,控制台窗口输出也是如此。

The solution provided here worked for me on Windows with R2018a.

In case link doesn't work: the idea is to use file matlabroot/bin/lcdata.xml to configure an alias for encoding name (some explanation can be found in this very file in the comments):

<codeset>
  <encoding name="UTF-8">
   <encoding_alias name="windows-1252" />
  </encoding>
</codeset>

You would use your own value instead of windows-1252, currently used encoding can be obtained by running feature('locale').

Although, if you use Unicode characters in help comments, the help browser does not recognize them, as well as console window output.

层林尽染 2024-10-24 13:51:28

对于 Mac OS 用户,Jendker 的解决方案确实有帮助!首先非常感谢。

在这里回顾一下。

  1. 通过在命令窗口中键入 getenv('LANG') 来检查 Matlab 中的默认语言。我的返回了 en_US.ISO8859-1

  2. 在Application目录中找到Matlab,显示其包内容。转到bin,以管理员身份打开lcdata.xml,找到相应的xpg_name,在我的例子中为en_US.ISO8859-1< /代码>。将同一行中的 encoding 更改为 UTF-8。保存。

  3. 重新启动Matlab,一切就完成了!

For Mac OS users, Jendker's solution really helps!!! Thanks a lot first.

Recap here.

  1. Check the default language in Matlab by typing in the command window getenv('LANG'). Mine returned en_US.ISO8859-1.

  2. In the Application directory find Matlab, show its package contents. Go to bin, open lcdata.xml as an administrator, locate the corresponding xpg_name, in my case en_US.ISO8859-1. Change encoding in the same line to UTF-8. Save it.

  3. Restart Matlab, and it's all done!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文