Remove specific field and "push up" its contents without losing the data.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Nathaniel H Werner
am 3 Dez. 2021
Kommentiert: Walter Roberson
am 5 Dez. 2021
I am loading a structure into MatLab and I would like to remove a field without losing the contents of that field. I'll explain exactly what I mean nowsince that may be confusing.
After loading the structure
Data.c1 = load(Data.mat);
The output is
Data.c1.Data.sN; % where I have 1-N "s" fields
I want to effectively convert this to
Data.c1.sN; % "pushing up" the "s" fields from 1-N
All the data within the lowest "s" fields would be moved up and take the place of the field Data in the variable Data.
Is there an easy way to do that?
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 3 Dez. 2021
temp = rmfield(Data.c1, 'Data');
fn_temp = fieldnames(Data.c1);
fn_c1D = fieldnames(Data.c1.Data);
combined_cell = [struct2cell(fn_temp); struct2cell(Data.c1.Data)];
combined_fn = [fn_temp;fn_c1D];
Data.c1 = cell2struct(combined_cell, combined_fn);
Obviously test this first !!
6 Kommentare
Walter Roberson
am 5 Dez. 2021
If you use
fieldnames(Data.c1)
then does anything other than Data show up ?
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!