Filter löschen
Filter löschen

save part of a stucture

49 Ansichten (letzte 30 Tage)
Daniel Boateng
Daniel Boateng am 1 Apr. 2019
Bearbeitet: Guillaume am 1 Apr. 2019
I have a matlab struct Data with these different fieldnames.Please how do I save just the fields (name, time and version) in both Data(1) and Data(2) without having to save all the struct. I tried
save('C:\danny\Pro', '-struct', 'Time','name','Version') % but it isnt working.
save('C:\danny\Pro', '-struct', 'Data', 'Time','name','Version')
Data(1).name = 'to be filled';
Data(1).Time = datestr(now);
Data(1).Project = 'LastProject.mat';
Data(1).SimulationParam = 2000;
Data(2).Version = 'Version 2.2';
Data(2).name = 'to be filled';
Data(2).Time = datestr(now);
Data(2).Project = 'LastProject.mat';
Data(2).SimulationParam = 2000;
Data(2).Version = 'Version 2.2';
save('C:\danny\Pro', '-struct', 'Data', 'Time','name','Version')

Akzeptierte Antwort

Guillaume
Guillaume am 1 Apr. 2019
Bearbeitet: Guillaume am 1 Apr. 2019
The -struct option of save saves each field of the structure as individual variables. Obviously for that to work the structure has to be scalar.
If you want to actually save the structure, then you don't want the struct option. To only save some fields, you'll have to either remove the unwanted fields or just copy the wanted fields into a new structure array. It's probably easier to remove the unwanted fields:
datatosave = rmfield(Data, setdiff(fieldnames(Data), {'Time', 'name', 'Version'})); %remove all fields but Time, name and Version
save('C:\danny\Pro', 'datatosave')

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by