MATLAB MATLAB char array with variable names

AI Thread Summary
The discussion revolves around reading data from a .hdf file in MATLAB and assigning it custom variable names. The user initially faced issues with dimension mismatches when trying to store multi-dimensional data into a character array. The problem was identified as an incorrect approach to indexing and data assignment. A solution was proposed by changing the variable names to a cell array and adjusting the loop to start from 1, which aligns with MATLAB's indexing system. The corrected code reads the data into a separate variable before using `eval` to assign it to the desired variable name. This method successfully resolved the user's issue with data storage and naming.
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.
 
Back
Top