saving multiple structures to a .mat file

46 views (last 30 days)
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')

Accepted Answer

Walter Roberson
Walter Roberson on 19 Feb 2013
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)

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!