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
回答 (1 件)
Geoff Hayes
2016 年 1 月 16 日
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 件のコメント
Dustin Gutsche
2016 年 1 月 16 日
Dustin Gutsche
2016 年 1 月 16 日
Dustin Gutsche
2016 年 1 月 17 日
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!