save part of a stucture

50 次查看(过去 30 天)
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')

采纳的回答

Guillaume
Guillaume 2019-4-1
编辑:Guillaume 2019-4-1
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')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by