How to avoid dynamic variable names with multiple structures?
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.
Accepted Answer
More Answers (0)
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!