I have a big number of MATLAB files initially developed in MATLAB on Windows, running under English (US) locale, so working with windows-1252 encoding, which I now want to use in MATLAB on Linux which is running under UTF-8 encoding. Is there a way to easily convert all these files to UTF-8 format?
MATLAB: How to batch convert windows-1252 encoded MATLAB files to UTF-8 encoding or vice versa
MATLAB
Related Solutions
From MATLAB, you can use the "movefile" function to rename files on your machine:
For more information on managing files and folders in MATLAB see this documentation:
Alternatively, you can use the "system" command which executes on the local operating system.
On Windows using CMD, the "rename" command works like this:
C:> rename file_path new_nameC:> rename C:\Temp\file1.txt file2.txt
So in MATLAB, from the directory with the file you could execute:
>> system("rename " + "old_name.txt" + " " + "new_name.txt")
If the file names contain spaces, this can confuse the operating system, so a more robust approach is to wrap each file name in double quotes like this:
>> system("rename " + '"' + "old name.txt" + '" "' + "new name.txt" + '"')
This produces a system command like this:
C:> rename "old name.txt" "new name.txt"
To use MATLAB variables for the old and new file names you can use these MATLAB commands:
>> old_path_to_file = "C:\Temp\test a.txt";
>> new_filename = "test b.txt";
>> cmd = "rename " + '"' + old_path_to_file + '" "' + new_filename + '"';
>> system(cmd);
On a Linux or macOS (Unix) system, this would be:
>> old_path_to_file = "/tmp/test a.txt";>> new_filename = "test b.txt";>> cmd = "mv " + '"' + old_path_to_file + '" "' + new_filename + '"';>> system(cmd);
This bug has been fixed in Release 14 Service Pack 3 (R14SP3). For previous product releases, read below for any possible workarounds:
MATLAB has a limited list of pre-defined countries for which it sets the default paper type to "A4" and default paper units to "centimeters". For all other countries, "usletter" and "inches" will be used respectively. These settings are created in the "hgrc.m" file.
There are two methods to resolve this issue and set the default value of these properties for your system.
1. If you are an individual user, especially on a network installation of MATLAB, it is recommended that you add the following line in your "startup.m" file (if a "startup.m" file does not exist on your system, create a new one and place it on the MATLAB path):
set(0,'DefaultFigurePaperType','A4')set(0,'DefaultFigurePaperUnits','centimeters')
This method is preferred for individual users since it will carry over between releases of MATLAB (upgrading MATLAB will preserve your "startup.m" file, but will over-write "hgrc.m"). This also allows "hgrc.m" and "matlabrc.m" to be used by network administrators to make system-wide changes without over-writing individual users' settings. This setting is also independent of your system's country setting, thus, even if your country is set to US, you can still use this method to set the default paper type and paper units in MATLAB.
2. If you are a network administrator and want to make a system-wide change, you can edit the "hgrc.m" file. The list of countries for which MATLAB sets the paper size to "A4" can be found in this file. To add your country code to this list, follow these steps:
a. Find the two-letter ISO 3166 short name for your country, i.e., the ISO 3166-1-alpha-2 code which can be found at the following address:
b. Type "edit hgrc" at the MATLAB prompt. (For reference, the "hgrc.m" file resides in $MATLAB\toolbox\local\, where $MATLAB is the MATLAB root directory; the file can be located on your system by typing "which hgrc" at the MATLAB prompt).
c. Scroll down to the line that reads
if usejava('jvm') && ~exist('defaultpaper','var') if any(strncmpi(char(java.util.Locale.getDefault.getCountry), ... {'gb', 'uk', 'fr', 'de', 'es', 'ch', 'nl', 'it', 'ru',... 'jp', 'kr', 'tw', 'cn', 'cz', 'sk', 'au', 'dk', 'fi',... 'gr', 'hu', 'ie', 'il', 'in', 'no', 'pl', 'pt',... 'ru', 'se', 'tr', 'za'},2)) defaultpaper = 'A4'; defaultunits = 'centimeters'; endend
d. You need to add your country code to this list. For example, if you were adding Liechtenstein (ISO 3166 code "li"), you would edit this code to read:
if usejava('jvm') && ~exist('defaultpaper','var') if any(strncmpi(char(java.util.Locale.getDefault.getCountry), ... {'gb', 'uk', 'fr', 'de', 'es', 'ch', 'nl', 'it', 'ru',... 'jp', 'kr', 'tw', 'cn', 'cz', 'sk', 'au', 'dk', 'fi',... 'gr', 'hu', 'ie', 'il', 'in', 'no', 'pl', 'pt',... 'ru', 'se', 'tr', 'za','li'},2)) defaultpaper = 'A4'; defaultunits = 'centimeters'; endend
e. Save the file.
f. Exit MATLAB and restart for the changes to take effect.
Related Question
- Do I receive an error message when using the command UNIX in MATLAB 6.0 (R12) on UNIX platforms
- How to obtain the coordinates of the mouse on the screen displayed somewhere in the figure window
- Convolution encoder
- How to call Cygwin command pipeline with the MATLAB ‘system’ function
- MacOSX encoding problem
Best Answer