MATLAB: Is it possible to read Japanese characters with datastore function

MATLAB

When reading the file of csv format like attached using the datastore function, garbled characters occurred. How do we avoid this issue?
ds=datastore('ja_sjis.csv')
ds.read
 

Best Answer

The function datastore doesn't support files encoded with Shift_JIS character code.
As a workarond, please use csv file of ASCII or UTF-8.
If Japanese characters are included, conversion to character code UTF-8 is possible using the following function (convertEncoding.m).
 
convertEncoding('ja_sjis.csv', 'Shift_JIS', 'UTF-8');
ds = datastore('ja_sjis.csv')
ds.read
 
% convertEncoding.m
%----------------------------------------------------------------------

%CONVERTENCODING Change the character encoding of a file
% CONVERTENCODING(FILENAME, CURRENTENCODING, NEWENCODING) converts the
% file FILENAME from character encoding CURRENTENCODING to a new
% encoding specified by NEWENCODING. A copy of the original version of
% FILENAME is placed at FILENAME.old.
%
% INPUT PARAMETERS:
% FILENAME: The name of the file to be converted.
% CURRENTENCODING: The encoding currently be used by FILENAME.
% NEWENCODING: The encoding to rewrite FILENAME in.
function convertEncoding(filename, currentEncoding, newEncoding)
bakFile = [filename, '.old'];
movefile(filename, bakFile);
fpIn = fopen(bakFile, 'r', 'n', currentEncoding);
fpOut = fopen(filename, 'w', 'n', newEncoding);
while feof(fpIn) == 0
lineIn = fgets(fpIn);
fwrite(fpOut, lineIn, 'char');
end
fclose(fpIn);
fclose(fpOut);
end
%----------------------------------------------------------------------