How do I Use a String to Call a value in a Struct?

So what I am trying to do is sort a large amount of point cloud data into more manageable sectors. I have created a slick function called Find_Sector which does this using kd-tree type approach. This function works fine.
What I am now trying to do is call this function in a loop that works through the entire point cloud. The issue I have is I had to create a struct called Sectors so that I could pre-allocate about 30 Gb for it (there are 864 sectors each with a million points available). I also created another struct called Next which just holds the index value for which point is available to use next in the Sectors so I don't overwrite it.
MY ISSUE:
I can't understand how to use the string that I create (which names the array in the Structs) to call the value I want to update. The error message I receive is that I am making a reference to a non-existent field 'string'. The error pops up for the line "Sectors.string(Next.string(1,1),1:3) = ..."
for i = 1:length(All_Data)
[X_Sector, Y_Sector] = Find_Sector(X_Start, Y_Start, Sector_Size, Dimensions(1,1), Dimensions(1,2), All_Data(i,1), All_Data(i,2));
string = genvarname(strcat('Sector_',num2str(Y_Sector),'_',num2str(X_Sector)));
% Create the Variable String
Sectors.string(Next.string(1,1),1:3) = All_Data(i,1:3);
% Copies the Entry to the Correct Sector
Next.string(1,1) = Next.string(1,1) + 1;
% Update to Which Entry is Next
end

Réponses (1)

Dustin - I think that you want to use the open brackets around the string name when trying to access a field within the struct (see generate field names from variables for details). For example,
myStruct.myField = 42;
myString = 'myField';
myStruct.(myString)*2
ans =
84
The above assigns an integer value to the myFieldName, and then we access it using the string variable myString.

3 commentaires

Hmmm I'll play with it a bit - but I'm having trouble understanding when my field is an array how to address that one entry of the field of the struct...
I'm hearing you both saying something similar so it must be on the right track.
I'm definitely having trouble figuring out how to reference the address INSIDE the field though - so my structure is 864 identical type fields. Each field contents a 1000000 x 3 array of data points. I don't know how to reference the line INSIDE the array INSIDE the field INSIDE the struct...
So for example my struct is Sectors. I want to access the field with the same name as String and then I want to update the next line within it. I can't figure out the syntax. :-(
Sectors.(string)(7,12)
Yay! It worked. I had to pass the value out to a variable, work off that and then passing it back in after the loop. Worked like a charm. Thanks all!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by