Effacer les filtres
Effacer les filtres

save part of a stucture

49 vues (au cours des 30 derniers jours)
Daniel Boateng
Daniel Boateng le 1 Avr 2019
Modifié(e) : Guillaume le 1 Avr 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')

Réponse acceptée

Guillaume
Guillaume le 1 Avr 2019
Modifié(e) : Guillaume le 1 Avr 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')

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by