"Access Elements of a Nonscalar Structure Array" issue

조회 수: 2 (최근 30일)
Thomas Fournier
Thomas Fournier 2021년 6월 7일
댓글: Jan 2021년 6월 7일
Hello,
I try to access data stored in a structure of this type:
I would love to have access easily for exemple to all the masses of all the subsystems, but i cannot manage to do it , i tried : (I pasted the full code at the end)
systems(:).subsystem.mass
but it returned: Reference to non-existent field 'subsystem'.
The real problem has a lot more system than that, do you have an idea on how i could access to all the masses contained in all the subsystems?
Thank you for your help!
I tried to follow this MatLab tutorial but it doesn't seem to work in my case:
clear all
clc
%% creation of the structure
systems=struct();
system1.mass=1000;
system1.CG=[0,1,2];
system1.subsystem.mass=[0;10;100];
system1.subsystem.CG=[0,1,2;3,4,5;6,7,8];
systems.system1=system1;
system2.CG=[0,1,2];
system2.mass=1000;
system2.subsystem.mass=[0;10;100];
system2.subsystem.CG=[0,1,2;3,4,5;6,7,8];
systems.system2=system2;
systems.mass=5000;
systems.CG=[10,20,30];
clear system1
clear system2
%% access to data
systems(:).subsystem.mass
  댓글 수: 2
Stephen23
Stephen23 2021년 6월 7일
편집: Stephen23 2021년 6월 7일
Why do you need to brutally CLEAR ALL cached functions from memory?
Is there a reason you cannot just use much more gentle CLEARVARS?
You did not create any non-scalar structures, so the documentation you linked to is not relevant:
isscalar(systems)
ans = logical
1
isscalar(systems.system1)
ans = logical
1
isscalar(systems.system2)
ans = logical
1
Possibly you are confusing the size of a structure with how many fields a structure has (whereas in fact these are totally independent from each other). The design of your scalar structures with multiuple fields does not offer any obvious trivial way to access the nested structures.
Thomas Fournier
Thomas Fournier 2021년 6월 7일
Thank you for your answer,
Indeed CLEARALL is not necessary, i recreated the problem in a separated file so it works just well this way.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2021년 6월 7일
You cannot access fields of deeply nested structs in Matlab. You need a loop to do this.
  댓글 수: 2
Thomas Fournier
Thomas Fournier 2021년 6월 7일
Thank you for your answers, I may have to reconsider how i build my structure then,
But if needed here is a solution which is not pretty but is working:
mass=[];
fn=fieldnames(systems);
for k=1:numel(fn)
if isstruct(systems.(fn{k}))
fn2=fieldnames(systems.(fn{k}));
for l=1:numel(fn2)
if string(fn2(l))=='subsystem'
mass_temp=systems.(fn{k}).subsystem.mass;
mass=[mass,mass_temp];
end
end
end
end
Jan
Jan 2021년 6월 7일
Hiding an index in the fieldname as in "system1" is a bad idea. Deeply nested structs are inefficient also, if you need to access nested fields. So restructuring the data might be the best approach.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2021년 6월 7일
편집: Stephen23 2021년 6월 7일
sys = fakedata()
sys = struct with fields:
system1: [1×1 struct] system2: [1×1 struct] mass: 5000 CG: [10 20 30]
fnm = regexp(fieldnames(sys),'^system\d+$','match');
fnm = [fnm{:}]
fnm = 1×2 cell array
{'system1'} {'system2'}
fun = @(f)sys.(f).subsystem.mass;
out = cellfun(fun,fnm,'uni',0)
out = 1×2 cell array
{3×1 double} {3×1 double}
Checking (tip for the future: using different values for fake data is a simple sanity check):
out{1}
ans = 3×1
0 10 100
out{2}
ans = 3×1
0 10 100

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by