Extract fields from structure

9 Ansichten (letzte 30 Tage)
Samy Alkhayat
Samy Alkhayat am 14 Aug. 2022
Kommentiert: Dyuman Joshi am 15 Aug. 2022
Hello,
I have a structure A that includes 2 structures, B and C. I want to extract from structure "C" , to the workspace, specific fields that start with certain set of letter, i.e I want to extract fields that start wih XX_YY.
I did
S=[A.C(:)]; save('param.mat','-struct','S');
But the file takes forever to load to the workspace.
Thanks in Advance!
  3 Kommentare
Samy Alkhayat
Samy Alkhayat am 15 Aug. 2022
I actually not sure how to extract directly to workspace without saving to a variable and loading it.
Dyuman Joshi
Dyuman Joshi am 15 Aug. 2022
It is, as the answers below show

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 15 Aug. 2022
A.C.XX_YY1 = 5;
A.C.XX_YY2 = 10;
A.C.ZZ = 3;
B = A.C;
F = fieldnames(B);
X = startsWith(F,'XX_YY');
B = rmfield(B,F(~X))
B = struct with fields:
XX_YY1: 5 XX_YY2: 10
  2 Kommentare
Samy Alkhayat
Samy Alkhayat am 15 Aug. 2022
Bearbeitet: Samy Alkhayat am 15 Aug. 2022
Works! Thank you!
@Stephen23 any way to put the fields inside "B" into the workspace directly?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 14 Aug. 2022
Bearbeitet: Matt J am 14 Aug. 2022
S.C.XX_YY1=5;
S.C.XX_YY2=10;
S.C.ZZ=3;
f=fieldnames(S.C);
I=startsWith(f,'XX_YY');
f=string(f(I));
for fI=string(f(I))'
Extraction.(fI)=S.C.(fI);
end
Extraction
Extraction = struct with fields:
XX_YY1: 5 XX_YY2: 10
  1 Kommentar
Samy Alkhayat
Samy Alkhayat am 15 Aug. 2022
@Matt J it works except I get the error below:
The logical indices contain a true value outside of the array bounds.
Any advice?
Thanks in advance!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by