Unicode characters in MATLAB source files - matlab

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?

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.

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.

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)
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!

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.

For Mac OS users, Jendker's solution really helps!!! Thanks a lot first.
Recap here.
Check the default language in Matlab by typing in the command window getenv('LANG'). Mine returned en_US.ISO8859-1.
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.
Restart Matlab, and it's all done!

Related

How to load a package in Maple?

I want to use a package called "ESC" elliptic surface calculator that can be uploaded with Maple.
The instructions from the creator are:
Save as a text file with character encoding ISO-8859-1 (ISO Latin 1)
and load within Maple using "read" command).
I have problem with uploading to Maple and saving with this encoding. Can anybody say the exact upload command with details and how to save text with this encoding?
Here is the package page: http://c-faculty.chuo-u.ac.jp/~kuwata/2012-13/Maple_resources/ESC.mpl
I use Windows 8.1 and Maple 18. Thanks!
On the webpage, just right click on the source code file and then click save as a text file.
After that, open maple work sheet and read the file ESC.mpl.txt, like this
restart:
read("C:/tcdata/ESC.mpl.txt") # I have save the file in a folder named `tcdata` in drive C.
Once the file is loaded/read in maple, you can do whatever you are suppose to do. I tried these, to check whether the source code is working or not.
ESC();
elliptic_surface(1,1,1,1,1);
Apparently, the source file has been read and is working properly.

Persian font encoding in m-files on Matlab [duplicate]

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?
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.
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.
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)
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!
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.
For Mac OS users, Jendker's solution really helps!!! Thanks a lot first.
Recap here.
Check the default language in Matlab by typing in the command window getenv('LANG'). Mine returned en_US.ISO8859-1.
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.
Restart Matlab, and it's all done!

How to change encoding of EMF model instances from ASCII to UTF-8 in Eclipse?

I am on working most of the time on Mac. I have changed the "Text file encoding" of my Eclipse instance from "Properties->Workspace" to UTF-8. But still, when I create EMF model instances (be it dynamic or from registered examples), the created *.xmi-files have always "ASCII" as their encoding. When I try to share these project containing the models, instances etc. over SVN, this causes some problems if I work with these later on a Windows machine. It is probably a Windows problem regarding the SVN. The exact error code can a later post here.
Still, from now on, I would like to work with UTF-8 anyways. So the question is, why are my created text files still encoded in ASCII, even though my Eclipse workspace property has been set to UTF-8?
I have also tried to change the property globally within eclipse.in with the following line: -Dfile.encoding=UTF-8
Another thing I was wondering, if the "New text file line delimiter" from "Properties->Workspace" set to default (in my case Unix) could have anything to do with this issue?
Thank you!
If you have an XML resource, the following might help:
XMLResource JavaDoc
-Martin

how can we identify notepad file?

how can we identify notepad files which is created in two computer, is there a any way to get any information about in which computer it was created.Or whether it is build in xp or linux.
If you right click on the file, you should be able to see the permissions and attributes of the file.
Check at the end of the line. Under GNU/Linux lines end with \n (ascii: 0x0A) while under Miscrosoft W$ndos it is \r\n (ascii: 0x0D 0x0A).
Wikipedia: https://en.wikipedia.org/wiki/Newline
found this: http://bit.ly/J258Mr
for identifying a word document but some of the info is relevant
To see on which computer the document had been created, open the Word
document in a hex editor and look for "PID_GUID". This is followed by
a globally unique identifier that, depending upon the version of Word
used, may contain the MAC address of the system on which the file was
created.
Checking the user properties (as already mentioned) is a good way to
see who the creator of the original file was...so, if the document was
not created from scratch and was instead originally created on another
system, then the user information will be for the original file.
Another way to locate the "culprit" in this case is to parse the
contents of the NTUSER.DAT files for each user on each computer. While
this sounds like a lot of work, it really isn't...b/c you're only
looking for a couple of pieces of information. Specifically, you're
interested in the MRU keys for the version of Word being used, as well
as perhaps the RecentDocs keys."
The one thing I can think on the top of my mind is inspecting the newline characters on your file - I'm assuming your files do have multiple lines. If the file was generated using Windows then a newline would be characterized by the combination of carriage return and line feed characters (CR+LF) whereas a simple line feed (LF) would be a hint that the file was generated in a Linux machine.
Right click one the file--> Details . You can see the computer name where it was created and the date.

Save currently running script in Matlab

Is there a way of saving the currently running script in Matlab? I have a script which automatically takes a backup of a set of scripts although if I have changed the current script then the saved version will be out of date.
Perhaps its possible to call some java?
Thanks
Somewhere on Yair Altman's site (see link in my other answer) he also referred to a blog entry about editorservices, which was introduced with MATLAB R2009b.
editorservices.getActive().save
should do what you want.
Okay, all I write here I learned from undocumentedmatlab.com by Yair Altman, in particular by looking into his EditorMacro.m... great stuff!
I'm assuming that Itamar Katz understood you correctly and that you are running unsaved code from the editor by using "Evaluate Cell" or "Evaluate Selection"; you want your code to realize that it is not saved and save the version currently displayed in the editor to some other location.
I have not found a way to save the file directly to the original location, but at least I have found a way to access the current text. You can then use fprintf to save it to wherever you want. I have tested this in Matlab 7.11 (R2010b); if you have a different version you'd need to dig through EditorMacro.m to find the correct code for Matlab 6.
if com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.isDirty
thisdocument=com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getDocument;
thisdocument_text=char(thisdocument.getText(0,thisdocument.getLength));
fid = fopen('backupfile.m','w');
fprintf(fid, '%s', thisdocument_text);
fclose(fid);
else
% saved file is unmodified in editor - no need to play tricks...
...
end
So the if-condition checks if the currently active editor window contains a file which is not saved ("dirty"); if it is, we need to retrieve the current version of the code (into variable thisdocument_text) and save this string to some file.
Does this help?