Extract data from struct array (2024)

61 Ansichten (letzte 30 Tage)

Ältere Kommentare anzeigen

Daniel Stein am 1 Jun. 2023

  • Verknüpfen

    Direkter Link zu dieser Frage

    https://de.mathworks.com/matlabcentral/answers/1976894-extract-data-from-struct-array

  • Verknüpfen

    Direkter Link zu dieser Frage

    https://de.mathworks.com/matlabcentral/answers/1976894-extract-data-from-struct-array

Bearbeitet: Stephen23 am 1 Jun. 2023

I have a struct array that each element contains substructures with data that I would like to extract into a numeric array or timeseries. The data structure is set up similar to the example I created below:

dt = 1/1000;

T = 5;

t = 0:dt:T;

data = struct([]);

for it = 1:length(t)

data(it).t = t(it);

data(it).sys = genStruct(t(it));

end

function sOut = genStruct(t)

sOut = struct;

sOut.sys1 = struct;

sOut.sys2 = struct;

sOut.sys1.sub1 = genSubsystemStruct(t);

sOut.sys1.sub2 = genSubsystemStruct(t);

sOut.sys2.sub1 = genSubsystemStruct(t);

sOut.sys2.sub2 = genSubsystemStruct(t);

end

function sOut = genSubsystemStruct(t)

sOut = struct;

sOut.t = t;

sOut.dataPoint1 = randn();

sOut.dataPoint2 = randn();

end

Since data(1:10).sys.sys1.sub1.dataPoint1 does not work, the only method I have found to get an array from this structure is to create a structure map and loop over struct as shown below:

sFieldsOfInterest = struct; % Data map

sFieldsOfInterest.data = ["t"]; % Fields listed under data are saved to a row

sFieldsOfInterest.sys = struct;

sFieldsOfInterest.sys.sys1 = struct('sub1', struct('data', ["dataPoint1", "dataPoint2"]), ...

'sub2', struct('data', ["dataPoint1", "dataPoint2"]));

sFieldsOfInterest.sys.sys2 = struct('sub1', struct('data', ["dataPoint1", "dataPoint2"]), ...

'sub2', struct('data', ["dataPoint1", "dataPoint2"]));

dataArray = [];

for ii = 1:length(data)

dataArray(end+1,:) = getDataFromStructArray(data(ii), sFieldsOfInterest);

end

function row = getDataFromStructArray(dataStruct, structure)

row = [];

f = fields(structure);

for ii = 1:length(f)

if f{ii} == "data"

fois = structure.(f{ii}); % Fields of interest

for foi = fois

row = [row dataStruct.(foi)];

end

else

row = [row getDataFromStructArray(dataStruct.(f{ii}), structure.(f{ii}))];

end

end

end

Is there a better way to access this data? For the most part, the data fields of interest are not multi-dimensional arrays.

0 Kommentare

-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Antworten (1)

Stephen23 am 1 Jun. 2023

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/1976894-extract-data-from-struct-array#answer_1248524

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/1976894-extract-data-from-struct-array#answer_1248524

Bearbeitet: Stephen23 am 1 Jun. 2023

In MATLAB Online öffnen

"Since data(1:10).sys.sys1.sub1.dataPoint1 does not work.."

First lets generate your sample structure:

dt = 1/1000;

T = 5;

t = 0:dt:T;

data = struct([]);

for it = 1:length(t)

data(it).t = t(it);

data(it).sys = genStruct(t(it));

end

Because all of your nested structures are scalar you can simply use ARRAYFUN:

F = @(s)s.sys.sys1.sub1.dataPoint1;

V = arrayfun(F, data)

V = 1×5001

-1.1297 0.6164 -0.6211 0.3362 0.0718 -0.4912 -0.1255 1.1036 -0.2441 -1.3993 -0.2652 0.0855 1.3243 -1.7874 -0.4221 -1.1474 0.4290 0.0323 -0.1947 -0.4415 0.4525 0.4684 0.4993 -0.3849 -0.7085 0.5279 2.2160 -0.6229 1.3110 -0.6714

Or you could use a few comma-separated lists:

s1 = [data.sys];

s2 = [s1.sys1];

s3 = [s2.sub1];

V = [s3.dataPoint1]

V = 1×5001

-1.1297 0.6164 -0.6211 0.3362 0.0718 -0.4912 -0.1255 1.1036 -0.2441 -1.3993 -0.2652 0.0855 1.3243 -1.7874 -0.4221 -1.1474 0.4290 0.0323 -0.1947 -0.4415 0.4525 0.4684 0.4993 -0.3849 -0.7085 0.5279 2.2160 -0.6229 1.3110 -0.6714

https://www.mathworks.com/help/matlab/matlab_prog/comma-separated-lists.html

https://www.mathworks.com/matlabcentral/answers/1656435-tutorial-comma-separated-lists-and-how-to-use-them

"For the most part, the data fields of interest are not multi-dimensional arrays."

What sizes do you expect the output to be? How do you want them concatenated together?

If using comma-separated lists then you will need to carefully consider using e.g. VERTCAT, HORZCAT, a cell array, ...

You may also find dynamic fieldnames useful:

https://www.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html

If you want complete flexibility (only gets one field value, but allows any number of nested sturctures, can define all levels and indexing via a cell array i.e. comma-separated list):

https://www.mathworks.com/help/matlab/ref/getfield.html

function sOut = genStruct(t)

sOut = struct;

sOut.sys1 = struct;

sOut.sys2 = struct;

sOut.sys1.sub1 = genSubsystemStruct(t);

sOut.sys1.sub2 = genSubsystemStruct(t);

sOut.sys2.sub1 = genSubsystemStruct(t);

sOut.sys2.sub2 = genSubsystemStruct(t);

end

function sOut = genSubsystemStruct(t)

sOut = struct;

sOut.t = t;

sOut.dataPoint1 = randn();

sOut.dataPoint2 = randn();

end

0 Kommentare

-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Siehe auch

Kategorien

MATLABLanguage FundamentalsData TypesStructures

Mehr zu Structures finden Sie in Help Center und File Exchange

Tags

  • struct
  • array

Produkte

  • MATLAB

Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Es ist ein Fehler aufgetreten

Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.


Translated by Extract data from struct array (3)

Extract data from struct array (4)

Website auswählen

Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .

Sie können auch eine Website aus der folgenden Liste auswählen:

Amerika

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europa

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asien-Pazifik

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Kontakt zu Ihrer lokalen Niederlassung

Extract data from struct array (2024)

References

Top Articles
Latest Posts
Article information

Author: Kieth Sipes

Last Updated:

Views: 5522

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.