How to avoid dynamic variable names with multiple structures?
19 views (last 30 days)
Show older comments
I have data that is extracted from a grib file using nctoolbox and arranged in to 7 structures of data, with 6 fields in each structure. The structures are arranged so that the data I want is in structureName.data, which is a matrix with tttt,xx,yy dimensions(ex 1824x34x27). In essence it is timestep x lon x lat.
Now, for each time step I want to pick out element x in the resulting 34x27 matrix. Ex for the first time step:
tempVar = structureName1.data(1, :, :);
tempVar= squeeze(tempVar);
valueIwant = tempVar(ind); % ind is the index of the value I want
And I want to do this for all structures. I know that dynamic variable names are a bad idea, but I just started understanding structures in matlab and I wonder if there is a good way to get that one specific element from all the time steps and all the structures? My first instinct is to do a for loop, trying to create a variable name that changes each loop to extract my data
ind = 56; %index I want to pick out of the matrix
nameVar = {structureName1 structureName2 ... structureName7}
for i = 1:7
for j = 1:size(structureName1.data,1)
tempVar = nameVar(i).data(j, :, :);
tempVar = squeeze(tempVar)';
value(i) = tempVar(ind);
end
newStruct.nameVar(i) = value;
end
end
I don't even know if that would have worked, and my other idea is to hardcode everything, which I usually try to avoid. I hope I managed to explain my problem in a way that is understandable.
0 Comments
Accepted Answer
Guillaume
on 16 Feb 2017
Assuming the structures are identical in term of field names, the best thing would be to create them as a single structure array. Failing that you can concatenate them after the fact:
sarray = [structureName1 structureName2 ... structureName7];
Using a structure array would simplify indexing. If the field names are different, then indeed a cell array as you've done is the way to go but again, I'd store the structures directly in the cell array at creation, so that the variables structureName1, etc. never exist.
I find your use of linear indexing for accessing your wanted element odd. It actually makes accessing the desired elements harder forcing you to use that tempVar (however, the squeeze is not needed). Why aren't using subscript indexing? I assume your required index started as a x,y coordinate. If not,
[x, y] = ind2sub([size(data, 3), size(data, 2)], ind); %index is row major instead of matlab's column major
You could then avoid the whole j loop. All the elements you want are:
data(:, x, y)
It's not clear from your post whether or not the data fields are all the same size. If they are, no loop is needed at all, just concatenate all the data matrices into one 4D array and use indexing
%assuming you've got all the structures as one array
data = cat(4, sarray.data);
wanteddata = squeeze(data(:, x, y, :));
%rows are timestep, columns are structures
If the matrices are different size, then I would do this:
wanteddata = arrayfun(@(s) s.data(:, x, y), sarray, 'UniformOutput', false)
%each cell is a column vector of timestep, one cell for each structure.
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!