Array of Structures (not structure array)
조회 수: 32 (최근 30일)
이전 댓글 표시
I have a problem to solve, I think an array of structures would solve my problem, but, it's not possible in matlab as far as I've seen. Please help me with a work around!
I do not want a structure array. I need independent names for the structures.
So I need to be able to do this:
asdf = struct('A', 3, 'B', 0)
zxcv = struct('A', 7, 'B', 0)
arr = [asdf , zxcv]
FUNCTION 1:
for i = 1 : length(arr)
arr(i).A = arr(i).B
end
FUNCTION 2:
for i = 1 : length(arr)
arr(i).B = arr(i).A
end
FUNCTION 3 (structure is called by name):
asdf.A = 65
FUNCTION 4:
var = asdf.A + zxcv.A
Function 3 is important, I can't just have a structure array because there are so many independtly named structures, and I have to add and/or remove many at a time, and often. So keeping track of that exact index number for asdf in a massive structure array, is a moving target, and every index would need to be verified in the code for structure in every location.
I've seen other solutions, have not found one that fits all three functions shown above.
Solutions I looked at:
1) This requires variables to be named the same: https://www.mathworks.com/matlabcentral/answers/385538-how-to-loop-through-a-set-of-variables-y1-y100
2) There was another thread, suggesting using a structure of strings of the names of the structures, but it doesn't allow you to write to the the structure, like in functions 1 and 2 above. I can't find the thread though. It was also answered by Stephen Cobeldick.
3) I even tried using eval with an array of strings named after the structures, but you can't write to eval, so that didn't work. https://www.mathworks.com/matlabcentral/answers/289850-not-sure-why-eval-name-of-a-variable-cannot-be-assigned-a-value
4) My only thought was to use this custom pointer library and use an array of pointers to the structures? I'm not super keen on using a custom library if matlab doesn't usually use pointers. https://www.mathworks.com/matlabcentral/answers/5798-array-pointers-in-matl
Any other solutions are welcome? Otherwise, maybe I need to switch to python or something.
Thanks in advance!
댓글 수: 4
Bruno Luong
2020년 9월 19일
편집: Bruno Luong
2020년 9월 19일
I largely recommend structure of array. Put A, B in (2 x D) arrays, column corresponding to variable names
arr = struct('name', ["asdf", "zxcv"], ...
'A', [3, 7], ...
'B', [0, 0]);
% function 1
arr.A = arr.B;
% function 2
arr.B = arr.A;
% function 3
arr.A(:,arr.name=="asdf") = 65
% function 4
var = sum(arr.A(:, ismember(arr.name, ["asdf", "zxcv"])))
Close to that struct of array above is table. But personally I don't care about table excepted when I want to display data nicely on screen or display data with uitable.
채택된 답변
Steven Lord
2020년 9월 18일
Consider a struct whose fields are themselves struct arrays.
arr.asdf = struct('A', 3, 'B', 2);
arr.zxcv = struct('A', 7, 'B', 5, 'C', 42);
You can access the fields in arr using dynamic field names
F = fieldnames(arr);
y = zeros(size(F));
Q = struct;
for k = 1:numel(F)
disp("Processing field " + F{k} + " of arr.")
y(k) = 2 * arr.(F{k}).A;
Q.(F{k}) = arr.(F{k}).B.^2;
end
disp('y is')
disp(y)
disp('Q is')
disp(Q)
arr.zxcv.B = arr.zxcv.A + 1;
추가 답변 (1개)
Matt J
2020년 9월 18일
편집: Matt J
2020년 9월 18일
I don't know if this is something you explored in (2), but if so, I don't see why it wouldn't cover what you are trying to do.
asdf = struct('A', 3, 'B', 0,'ID',"asdf")
zxcv = struct('A', 7, 'B', 0, 'ID',"zxcv")
arr = [asdf , zxcv];
arr([arr.ID]=="asdf").A=65;
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!