MATLAB char array with variable names

Click For Summary
SUMMARY

This discussion addresses the challenge of assigning variable names to multi-dimensional data read from a .hdf file in MATLAB. The original approach using a character array for variable names led to dimension mismatch errors. A solution was proposed that involves using a cell array for variable names and utilizing the eval function to dynamically assign the data to the desired variable names. The corrected code effectively resolves the issue by ensuring proper indexing and data handling.

PREREQUISITES
  • Familiarity with MATLAB programming and syntax
  • Understanding of .hdf file format and data structures
  • Knowledge of MATLAB's eval function and its implications
  • Experience with multi-dimensional arrays in MATLAB
NEXT STEPS
  • Explore MATLAB's cell arrays for dynamic data storage
  • Learn about MATLAB's hdfsd functions for handling .hdf files
  • Investigate best practices for using eval in MATLAB to avoid potential pitfalls
  • Study MATLAB's indexing rules, particularly for multi-dimensional arrays
USEFUL FOR

MATLAB users, data scientists, and researchers working with climate data or other multi-dimensional datasets stored in .hdf format.

Halsey
Messages
3
Reaction score
0
Hello all.

I am having troubles reading in data(from a .hdf file) and giving it my own variable names. I have created a character array var_names with the names I wish to use. The .hdf variables are 3D and 4D (they are climate parameters). What I am trying to do is loop through the variables in the .hdf file and assign them my desired name in Matlab. At the end, I realize that the var_name(a,:) = ... is trying to put a multi-D array into the 2D var_name variable. This is not what I want, I want the var_name(a,:) to provide the variable name that the .hdf data corresponds to and accept the multi-D .hdf array. If you are not familiar with .hdf format, not to worry, the problem isn't with reading the data in, but storing it in the correct variable name. Thanks for the help!



var_names = char('sea_p', 'sfc_p', 'sfc_geop', 'geop_h', 'o3_mix', 'sp_hum', 'cld_l_mix', 'cld_i_mix', 'rel_hum', 'temp', 'u', 'v', 'pot_vort', 'omega', 'long', 'lat', 'height', 'time');

for a = 0:17
data_id = hdfsd('select', file_id, a);
[name, num_dim, dim_vect, data_type, num_attr_data, status] = hdfsd('getinfo', data_id);
start_vect = zeros(size(dim_vect));
end_vect = dim_vect;
step = [];
var_names(a,:) = hdfsd('readdata', data_id, start_vect, step, end_vect);
end
 
Physics news on Phys.org
==> I just realized that the last line should be var_names(a+1,:) because Matlab indices begin with 1, but that is not the problem.

err msg is:


Warning: Out of range or non-integer values truncated during conversion to
character.
> In read_hdf at 20
? Subscripted assignment dimension mismatch.

Error in ==> read_hdf at 20
var_names(a+1,:) = hdfsd('readdata', data_id, start_vect, step, end_vect);
 
I´ve been struggeling with the same problem.
Here is a solution to the problem.

var_names = {'sea_p' 'sfc_p' 'sfc_geop' 'geop_h' 'o3_mix' 'sp_hum' 'cld_l_mix' 'cld_i_mix' 'rel_hum' 'temp' 'u' 'v' 'pot_vort' 'omega' 'long' 'lat' 'height' 'time'); %made it into a row instead.

for a = 1:17 %makes more sense as you referr to a in data_id
data_id = hdfsd('select', file_id, a);
[name, num_dim, dim_vect, data_type, num_attr_data, status] = hdfsd('getinfo', data_id);
start_vect = zeros(size(dim_vect));
end_vect = dim_vect;
step = [];
S=hdfsd('readdata', data_id, start_vect, step, end_vect); % made it separate just to make it more visible
eval([var_names(a) '=S']);
end

That made the trick for me.