Array of Structures (not structure array)

4 views (last 30 days)
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:
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 Comments
Bruno Luong
Bruno Luong on 19 Sep 2020
Edited: Bruno Luong on 19 Sep 2020
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.
AlexMatLab
AlexMatLab on 20 Sep 2020
Thank you for your response, but I think for my case, Steven's answer better solves my problem.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 18 Sep 2020
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;

More Answers (1)

Matt J
Matt J on 18 Sep 2020
Edited: Matt J on 18 Sep 2020
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;
  8 Comments
AlexMatLab
AlexMatLab on 20 Sep 2020
Steven,
Ok, this makes a lot of sense. I think this is a really good solution.
Thank you Steven!
Good point about var variable name.
AlexMatLab
AlexMatLab on 20 Sep 2020
Thank you everyone for your responses.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!