MATLAB char array with variable names

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 13K views
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
 
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.