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

9 vues (au cours des 30 derniers jours)
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?

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 2 Mar 2020
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.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by