MATLAB: How to batch convert windows-1252 encoded MATLAB files to UTF-8 encoding or vice versa

MATLAB

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?

Best Answer

There is no built-in feature in MATLAB to do this, nor is there any MathWorks provided tool which would perform this conversion. There are many third-party command line tools available however which can perform these conversions. On Linux you for example have the iconv tool, based on which you could write a shell script like the following:
#!/bin/bash
find $1 -name "*.m" -exec bash -c 'mkdir -p "$0/`dirname "{}"`"; iconv -f windows-1252 -t utf-8 "{}" > "$0/{}"' "$2" \;
Which takes two directories as input: the directory containing the original files and an output directory in which to recreate the directory structure with M-files converted from windows-1252 to UTF-8 encoding. To perform conversion in the opposite directory swap the 'windows-1252' and 'utf-8' parameters.
You may even also be able to run this script on Windows if you have these "Unix Tools" installed on Windows (for example as part of cygwin or Git Bash).
Doing an online search on 'batch converting file encoding' will give you many more options/variants on this topic.