Home > Archive > Matlab > January 2008 > Save and load data in a GUI
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Save and load data in a GUI
|
|
| Henrik Odéen 2007-12-07, 4:36 am |
| Hi,
I want to be able to save and then later load data with my
GUI but I haven't found any good way of doing this. What I
basically want to do is to be able to save all the
variables in the workspace, and then later be able to open
the GUI and load in all the values I had the last time and
also be able to continue working with tha variables I had
in my workspace.
I've tried using 'uisave' to save a .mat file with
everything in the workspace, and then try to load it
using 'uiload' but can't get it to work. Plus in this way I
won't get the values back in the GUI again.
I'd be very happy if anyone could help me!!!
| |
| Lars Warmbold 2007-12-07, 4:36 am |
| >> save('mydata.mat')
| |
| per isakson 2007-12-07, 8:19 am |
| "Henrik O" <f03ho@student.lth.se> wrote in message <fjb7f6
$9hl$1@fred.mathworks.com>...
> "Lars Warmbold" <mustermann.klaus.TO.REMOVE@gmx.de> wrote
> in message <fjb57k$9lm$1@fred.mathworks.com>...
>
> Thanks, but this just makes the same thing as if I use
> uisave, just that I don't get to choose where to save
> the .mat file. I'm sorry if it wasn't clear but my
problem
> when using uisave (or save('mydata.mat') for that matter)
> is when I try to load the data again using uiload.
> If I close my GUI and open it later I want to be able to
> load the data from the saved workspace but i also want
the
> settings in the GUI to be set as when I saved it. Does
> anyone know if there's a build-in function that does this?
> Thanks!
>
Try hgsave and hgload / per
| |
| Yumnam Kirani Singh 2007-12-07, 8:19 am |
| The best thing is to load your data in the gui-workspace using the GUI handles. See help on guidata.
| |
| Kelly Eng 2008-01-30, 11:15 pm |
| Hi,
I'm also facing the same problem. Did you eventually find
out how to resolve the problem?
Cheers,
K
| |
| turtie 2008-01-30, 11:15 pm |
| for any general GUI, add two pushbuttons:
function save_pushbutton_Callback(hObject, eventdata, handles)
hgsave('myData');
function load_pushbutton_Callback(hObject, eventdata, handles)
helloWorld = gcf;
hgload('myData');
close(helloWorld);
that should do the trick
| |
| Henrik O 2008-01-31, 5:27 am |
| "Kelly Eng" <seowhwang@hotmail.com> wrote in message <fnr855
$nru$1@fred.mathworks.com>...
> Hi,
>
> I'm also facing the same problem. Did you eventually find
> out how to resolve the problem?
>
> Cheers,
> K
Hi,
I solved it by making a cell array (called "fieldRecord"
below) where I saved the name of all my buttons, sliders
etc. Then I have a loop where I save the Value, Enable,
String and Visible for all these into a structure (called
property below). At the end I use the save command to save
all the workspace variables. To load the GUI I pretty much
do the same thing but use "set" instead of "get".
Hope this help!
Code to save GUI:
fieldRecord = {'PushButton1','Slider1'...}
saveStructure.fieldRecord = fieldRecord;
for i = 1:length(fieldRecord)
property(i).Value = get(getfield(handles,fieldRecord
i}),'Value');
property(i).Enable = get(getfield(handles,fieldRecord
{i}),'Enable');
property(i).String = get(getfield(handles,fieldRecord
{i}),'String');
property(i).Visible = get(getfield(handles,fieldRecord
{i}),'Visible');
end
saveStructure.property = property;
save(fileName,'saveStructure');
Code to load GUI:
[fname pname] = uigetfile('*.mat','Select the GUI to load');
load([pname fname]);
%Restor the GUI
fieldRecord = saveStructure.fieldRecord;
property = saveStructure.property;
for i = 1:length(fieldRecord)
set(getfield(handles,fieldRecord{i}),'Va
lue',property
(i).Value);
set(getfield(handles,fieldRecord{i}),'En
able',property
(i).Enable);
set(getfield(handles,fieldRecord{i}),'St
ring',property
(i).String);
set(getfield(handles,fieldRecord{i}),'Vi
sible',property
(i).Visible);
end
saveStructure= rmfield(saveStructure,'property');
fields = fieldnames(saveStructure);
for i = 1:length(fields);
handles = setfield(handles,fields{i},getfield
(saveStructure,fields{i}));
end
|
|
|
|
|