reading hdf5 file with dataset name containing special characters

1 view (last 30 days)
I have a hdf5 file, hdf5disp give me the following
Group '/'
..........
Group '/histogram_plots'
Group '/histogram_plots/last_plot'
......
Dataset '23: Total Intensity = |Eσ|² + |Eπ|²'
Size: 400
MaxSize: 400
Datatype: H5T_IEEE_F64LE (double)
ChunkSize: []
Filters: none
FillValue: 0.000000
note the Dataset '23: Total Intensity = |Eσ|² + |Eπ|²', obviously the following line works in matlab console but not in a script. What can be the solution?
y= h5read(fname,'/histogram_plots/last_plot/23: Total Intensity = |Eσ|² + |Eπ|²');

Answers (1)

Walter Roberson
Walter Roberson on 22 Sep 2022
If the command works in a console but not in a script, and you are using an old enough version of MATLAB (I see you are using R2018b which is a bit older), then potentially your MATLAB is not saving .m files as UTF-8 and so might be getting incorrect characters.
If so, then there are a couple of approaches you can take:
  • you could upgrade to R2020a or newer, which changes the default encoding to UTF-8 for all operating systems
  • you could use one of the methods for convincing your older MATLAB to encode the file in UTF-8 . If I recall correctly, that is most difficult on Windows
  • you could construct the character vector without using the special characters in the code
  • you could create a uint8 stream of the utf8 bytes that encode the characters, and use native2unicode to convert into character vector. This is a quite effective way, but it is not readable
sigma_char = char(hex2dec('03C3')); %https://www.compart.com/en/unicode/U+03C3
pi_char = char(hex2dec('03C0')); %https://www.compart.com/en/unicode/U+03C0
raised_2_char = char(hex2dec('00B2')); %https://www.compart.com/en/unicode/U+00B2
Dataset = sprintf('/histogram_plots/last_plot/23: Total Intensity = |E%s|%s + |E%s|%s', sigma_char, raised_2_char, pi_char, raised_2_char);
y= h5read(fname, Dataset);

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!