MATLAB char array with variable names

In summary, the user is having trouble reading and assigning variable names to data from a .hdf file in Matlab. They have created a character array with the desired names and are attempting to loop through and assign the names to the data. However, they are receiving an error message and are unsure how to fix it. The solution provided is to separate the last line and use the "eval" function to properly assign the names.
  • #1
Halsey
3
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
  • #2
==> 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);
 
  • #3
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.
 

1. What is a char array in MATLAB?

A char array in MATLAB is a data structure that contains a sequence of characters. It is similar to a string in other programming languages, but in MATLAB, the characters are stored in a matrix format, with each row representing a string and each column representing a character within that string.

2. How can I create a char array with variable names in MATLAB?

To create a char array with variable names in MATLAB, you can use the char function and assign it to a variable name. For example, my_array = char('variable1', 'variable2', 'variable3').

3. Can I assign a value to a specific element in a char array with variable names?

Yes, you can assign a value to a specific element in a char array with variable names using indexing. For example, my_array(2,3) = 'a' will assign the character a to the third element in the second row of the array.

4. How do I access a specific element in a char array with variable names?

To access a specific element in a char array with variable names, you can use indexing. For example, my_array(2,3) will return the third element in the second row of the array.

5. Can I convert a char array with variable names to a cell array in MATLAB?

Yes, you can convert a char array with variable names to a cell array in MATLAB using the cellstr function. For example, my_cell_array = cellstr(my_array) will convert the char array my_array to a cell array my_cell_array.

Back
Top