필터 지우기
필터 지우기

saving multiple structures to a .mat file

조회 수: 34 (최근 30일)
Ruben
Ruben 2013년 2월 19일
I am trying to save multiple structures to one .mat file. This is needed because I am interfacing Matlab and GAMS (an optimization solver). However, when I run the code you see below, i get the message
'Error using save Saving from multiple structures is not supported.'
Does anyone know a way around this?
%%Pelmax
Pelmax.name='Pelmax';
Pelmax.val=[0; 0; 0; 0;];
Pelmax.form='full';
Pelmax.type='parameter';
%%Pelmin
Pelmin.name='Pelmin';
Pelmin.val=[0; 0; 0; 0;];
Pelmin.form='full';
Pelmin.type='parameter';
%%Pthmax
Pthmax.name='Pthmax';
Pthmax.val=[0; 0; 0; 0;];
Pthmax.form='full';
Pthmax.type='parameter';
%%Pthmin
Pthmin.name='Pthmin';
Pthmin.val=[0; 0; 0; 0;];
Pthmin.form='full';
Pthmin.type='parameter';
%%a and b (relation Pel and Pth)
a_P.name='a_P';
a_P.val=[0; 0; 0; 0;];
a_P.form='full';
a_P.type='parameter';
b_P.name='b_P';
b_P.val=[0; 0; 0; 0;];
b_P.form='full';
b_P.type='parameter';
%%a and b (relation eta_el and Pel)
a_el.name='a_el';
a_el.val=[0; 0; 0; 0;];
a_el.form='full';
a_el.type='parameter';
b_el.name='b_el';
b_el.val=[0; 0; 0; 0;];
b_el.form='full';
b_el.type='parameter';
%%a and b (relation eta_th and Pth)
a_th.name='a_th';
a_th.val=[0; 0; 0; 0;];
a_th.form='full';
a_th.type='parameter';
b_th.name='b_th';
b_th.val=[0; 0; 0; 0;];
b_th.form='full';
b_th.type='parameter';
%%min up
minup.name='minup';
minup.val=[0; 0; 0; 0;];
minup.form='full';
minup.type='parameter';
%%min down
mindown.name='mindown';
mindown.val=[0; 0; 0; 0;];
mindown.form='full';
mindown.type='parameter';
%%save everything in one .mat-file
save('CHPS','-struct', 'Pelmax',...
'-struct', 'Pelmin',...
'-struct', 'Pthmax',...
'-struct', 'Pthmin',...
'-struct', 'a_P', ...
'-struct', 'b_P', ...
'-struct', 'a_el', ...
'-struct', 'b_el', ...
'-struct', 'a_th', ...
'-struct', 'b_th', ...
'-struct', 'minup', ...
'-struct', 'mindown')

채택된 답변

Walter Roberson
Walter Roberson 2013년 2월 19일
save -struct is for the case where each field in the structure should become a new variable in the file, with the structure container removed. For example,
mytime.hour = 17; mytime.minute = 52;
save Example -struct mytime
would produce a .mat file with top-level variables "hour" and "minute".
Using this kind of expansion with multiple structures at the same time is not supported. (Suppose more than one structure had the same field name... what then?)
If you want to save structures with the structuring intact, do not use the -struct flag. For example,
mytime.hour = 17; mytime.minute = 52;
save Example mytime
would produce a .mat file with the top-level variable "mytime" that is a structure. You can save multiple structures as structures.
If you need to save multiple structures with the structure level removed, then you will have to create a single structure will all of the fields. You might find struct2cell useful for that (concatenate all the cells that result, then cell2struct() back into a single structure)

추가 답변 (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