Kathi
Kathi

Reputation: 99

Error in MATLAB when opening .mat files with Chinese characters written by savemat

It looks like savemat is producing .mat files that MATLAB cannot open when they contain Chinese characters, but loadmat is able to:

$ python -c 'from scipy.io import *; savemat("tmp.mat", {"test": "你好"}); print(loadmat("tmp.mat")["test"])' && matlab -nodesktop -r 'load tmp; exit'
['你好']

To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.

Error using load
Can't read file /home/tmp.mat.

Any ideas how I can either change savemat to output something MATLAB can open or change MATLAB settings such that I can read the files?

Upvotes: 3

Views: 413

Answers (1)

Dev-iL
Dev-iL

Reputation: 24179

Based on the documentation of savemat and save I believe that savemat produces .mat files with an older format (what MATLAB calls -v6, that doesn't support Unicode (introduced in -v7).

You should be able to save .mat files of the newer format if you switch from savemat to an HDF5 writer like h5py or hd5storage. Alternatively, you can always store strings as byte arrays and interpret them later on.

See also: Creating a .mat file of v7.3 in python, Strings in HDF5.

Upvotes: 0

Related Questions