save part of a stucture

조회 수: 26 (최근 30일)
Daniel Boateng
Daniel Boateng 2019년 4월 1일
편집: Guillaume 2019년 4월 1일
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