How can I save the contents of a struct to a .mat file?

조회 수: 333 (최근 30일)
Konstantin Davy
Konstantin Davy 2016년 6월 24일
답변: karipuff 2023년 4월 26일
I have a 1x1 struct called dataout. It consists of several fields, each containing a cell array.
>> dataout
dataout =
field1: {6x6 cell}
field2: {6x6 cell}
field3: {6x6 cell}
field4: {6x6 cell}
I want to export this struct to a .mat file so that a = load('file.mat') will result in a struct-variable a containing all fields.
So in the end I want a .mat file which will fulfill the following:
>> a = load('file.mat')
a =
field1: {6x6 cell}
field2: {6x6 cell}
field3: {6x6 cell}
field4: {6x6 cell}
I have tried save(filename,'dataout') but this results in a .mat-file that reacts differently:
>> save('file.mat','dataout');
>> b = load('file.mat')
b =
dataout: [1x1 struct]
Can somebody help me?

채택된 답변

Stephen23
Stephen23 2016년 6월 24일
편집: Stephen23 2016년 6월 24일
Solution
This is exactly what load does when it has an output variable: it loads into one structure, where each field is one of the variables that were saved in the .mat file. So when you save only one variable dataout, the output of load is a structure with one field: dataout (your structure, but it could be anything). You can access your structure in the usual way:
S = load('file.mat');
b = S.dataout;
Here is a complete working example of this:
>> old.a = 12.7;
>> old.b = NaN;
>> old.c = 999;
>> save('test.mat','old')
>> S = load('test.mat');
>> new = S.old; % <- yes, you need this line!
>> new.a
ans = 12.700
Alternative
As an alternative you can actually change the save call to get exactly the effect that you desire, by using the '-struct' option:
>> save('test.mat','-struct','old') % <- note -struct option
>> new = load('test.mat');
>> new.c
ans = 999
This simply places all of the fields of the input scalar structure as separate variables in the mat file. Read the help to know more.
  댓글 수: 2
Konstantin Davy
Konstantin Davy 2016년 6월 24일
Thanks for your answer.
This works - but it would require me rewriting a couple of programs in our company.
Our measurement devices already export .mat-files that react as I described when being loaded, and I now want to create similar files. (I am converting data from an old format into .mat-files, and the resulting files should be in the same format as the files produced by the measurement devices)
Konstantin Davy
Konstantin Davy 2016년 6월 24일
Ah, I think this solves it - I need to store all fields of my dataout-structure seperately into the same file.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

karipuff
karipuff 2023년 4월 26일
%%% Saving content of structure
field_str = fieldnames(a);
save('filename.mat', field_str{:})
%% Loading content of structure
b = load('filename.mat');
b =
field1: {6x6 cell}
field2: {6x6 cell}
field3: {6x6 cell}
field4: {6x6 cell}

카테고리

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