Thread Subject: GUI - reopening previous run

Subject: GUI - reopening previous run

From: Jason

Date: 9 Jan, 2008 10:44:02

Message: 1 of 10

Hello,

I have the following problem with using Matlab GUI. I have
interfaced my GUI with SIMULINK and it works very well.

My problem is that the GUI is large. Therefore every-time I
reopen it I do not want to type the individual values for
each variable for a previous run. I want the values from the
previous run to be viewable in my GUI

Presently I have the values that are defined in the "edit
text" box as my "start up" values. Obviously these values
are determined by what is in "string" under uicontrol.

Is there any way I can make some sort of call to the
variable in the workspace I have loaded from a previous run.
Or is there another way?

Thanks

Subject: GUI - reopening previous run

From: Anh Huy Phan

Date: 9 Jan, 2008 11:06:01

Message: 2 of 10

"Jason " <j_henderson44@REMOVEhotmail.com> wrote in message
<fm28hi$kot$1@fred.mathworks.com>...
> Hello,
>
> I have the following problem with using Matlab GUI. I have
> interfaced my GUI with SIMULINK and it works very well.
>
> My problem is that the GUI is large. Therefore every-time I
> reopen it I do not want to type the individual values for
> each variable for a previous run. I want the values from
the
> previous run to be viewable in my GUI
>
> Presently I have the values that are defined in the "edit
> text" box as my "start up" values. Obviously these values
> are determined by what is in "string" under uicontrol.
>
> Is there any way I can make some sort of call to the
> variable in the workspace I have loaded from a previous
run.
> Or is there another way?
>
> Thanks

It could be done like this

   Save data of your GUI into file before it's deleted.
   Then check and load data file for the next time.

In this case, your components could get the default values
or the user-defined values.

Anh Huy Phan
RIKEN - BSI



Subject: GUI - reopening previous run

From: Jason

Date: 9 Jan, 2008 12:06:02

Message: 3 of 10

"Anh Huy Phan" <phananhhuy@mathworks.com> wrote in message
<fm29qp$g65$1@fred.mathworks.com>...
> "Jason " <j_henderson44@REMOVEhotmail.com> wrote in message
> <fm28hi$kot$1@fred.mathworks.com>...
> > Hello,
> >
> > I have the following problem with using Matlab GUI. I have
> > interfaced my GUI with SIMULINK and it works very well.
> >
> > My problem is that the GUI is large. Therefore every-time I
> > reopen it I do not want to type the individual values for
> > each variable for a previous run. I want the values from
> the
> > previous run to be viewable in my GUI
> >
> > Presently I have the values that are defined in the "edit
> > text" box as my "start up" values. Obviously these values
> > are determined by what is in "string" under uicontrol.
> >
> > Is there any way I can make some sort of call to the
> > variable in the workspace I have loaded from a previous
> run.
> > Or is there another way?
> >
> > Thanks
>
> It could be done like this
>
> Save data of your GUI into file before it's deleted.
> Then check and load data file for the next time.
>
> In this case, your components could get the default values
> or the user-defined values.
>
> Anh Huy Phan
> RIKEN - BSI
>
>
>

Hello,

Yes this is the way I have been trying to do it. However I
am not sure how to load the user-defined values (saved from
last run) instead of my default values.

In the uicontrol for the variable I have a box for "string".
This is the value I have when the GUI opens. How do I direct
this string to the variable from the workspace/file?

Thanks.

Subject: GUI - reopening previous run

From: Anh Huy Phan

Date: 9 Jan, 2008 13:36:02

Message: 4 of 10


> Yes this is the way I have been trying to do it. However I
> am not sure how to load the user-defined values (saved from
> last run) instead of my default values.

Something likes that-
    Check the existence of your user-defined data file,
    If yes, load this file, and set values to your control
objects.
    Otherwise, set them by default values.
 
    datafile = 'uservals.mat';
    if exist(datafile,'file)==2
        %% load your data
        uservals = load(datafile);%load(datafile) is also OK
        name = fieldnames(data);

        %% set user-defined value
        set(handles.object1, 'Value',getfield(data,name{1}))
        ....
        
    else
        %% set default value
        set(handles.object1, 'Value',value1)
        ...
    end



> In the uicontrol for the variable I have a box for
"string".
> This is the value I have when the GUI opens. How do I
direct
> this string to the variable from the workspace/file?

Do you want to get and assign the string in the edit text to
one variable in the workspace???
    obj = findobj('parent',gcf,'style','edit');
    str = get(obj,'String');

also you could use the functions assignin and evalin.

Anh Huy Phan
RIKEN - BSI

Subject: GUI - reopening previous run

From: Jason

Date: 9 Jan, 2008 15:18:02

Message: 5 of 10

"Anh Huy Phan" <phananhhuy@mathworks.com> wrote in message
<fm2ik1$pn3$1@fred.mathworks.com>...
>
> > Yes this is the way I have been trying to do it. However I
> > am not sure how to load the user-defined values (saved from
> > last run) instead of my default values.
>
> Something likes that-
> Check the existence of your user-defined data file,
> If yes, load this file, and set values to your control
> objects.
> Otherwise, set them by default values.
>
> datafile = 'uservals.mat';
> if exist(datafile,'file)==2
> %% load your data
> uservals = load(datafile);%load(datafile) is also OK
> name = fieldnames(data);
>
> %% set user-defined value
> set(handles.object1, 'Value',getfield(data,name{1}))
> ....
>
> else
> %% set default value
> set(handles.object1, 'Value',value1)
> ...
> end
>
>
>
> > In the uicontrol for the variable I have a box for
> "string".
> > This is the value I have when the GUI opens. How do I
> direct
> > this string to the variable from the workspace/file?
>
> Do you want to get and assign the string in the edit text to
> one variable in the workspace???
> obj = findobj('parent',gcf,'style','edit');
> str = get(obj,'String');
>
> also you could use the functions assignin and evalin.
>
> Anh Huy Phan
> RIKEN - BSI
>

Hello Anh Huy Phan and thank you for your help.

I am still struggling to implement this. I have been using
SIMULINK so I am only a novice on GUI.

I am now trying to do my task with a more simple GUI. In
mygui.fig I have two variables.

To execute my gui I have directed a pushbutton to a .m file
in the callback uicontrol. so I have a .m file with the lines

_____________________________

%notice that this m-file is NOT a function because
%simulink only allows you to use variables that are within
the main
%workspace
clear all

%make the handles structures available to the main workspace
h =gcf;
handles = guidata(h);

%set the axes to which the data will be plotted to
axes(handles.axes1);

datafile = 'workspace.mat'
if exist(datafile,'file')==2
load(datafile)
end

% get the parameters from the edit text fields
mass=str2num(get(handles.mass,'String'));


%simulate the system
sim('testmodels');

___________________________________

This creates the variable in workspace to pass to SIMULINK.

As you can see I have added your lines of code. However I am
not sure if I should add your other code here or in mygui.m
As you can see I get the parameters from the edit text field
so I just need to set up the if loop correctly. Should I set
it up here or in the mygui.m file. I am not sure of best
practice. Therefore I am not sure how to implement your code
below? Any advice is appreciated.

 Thanks


name = fieldnames(data)
set(handles.object1, 'Value',getfield(data,name{1}))
else
        set(handles.object1, 'Value',value1)
      
end



_____________________________________

function mass_Callback(hObject, eventdata, handles)
% hObject handle to mass (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of mass as text
% str2double(get(hObject,'String')) returns contents
of mass as a double


% --- Executes during object creation, after setting all
properties.
function mass_CreateFcn(hObject, eventdata, handles)
% hObject handle to mass (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles empty - handles not created until after all
CreateFcns called

% Hint: edit controls usually have a white background on
Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




Subject: GUI - reopening previous run

From: Anh Huy Phan

Date: 9 Jan, 2008 17:35:03

Message: 6 of 10

TO Jason:

Basically, your purpose is to get values from the control
objects ('edit text') in GUI, and assign them to variables
in workspace. And you also want to save data in your GUI.

In the OpeningFcn function, you check the existence of your
data file and set value to your edit text. It looks like that

function mygui_OpeningFcn(hObject, eventdata, handles)
handles.output = hObject;
handles.datafile =fullfile(pwd,'workspace.mat');
data = [123,4567]; %default data edit1: 123, edit2:4567
if exist(handles.datafile,'file')==2
    load(handles.datafile); %contains userdata variable
    data = userdata;
end
%set values to edittext objects
set(handles.edit1,'string',num2str(data(1)));
set(handles.edit2,'string',num2str(data(2)));


% Update handles structure
guidata(hObject, handles);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



Assume that 'mass' object is button to get data, here is its
Callback function (you don't need to run other script file).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mass_Callback(hObject, eventdata, handles)
% get data
data = str2double(get([handles.edit1,
handles.edit2],'string'));

% assign to variables in workspace
assignin('base','var1',data(1));
assignin('base','var2',data(2));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

And you also need to code the CloseRequestFcn function of
your figure.


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)

% update variables in workspace
% mass_Callback(handles.mass, eventdata, handles)

% get data and save
userdata = str2double(get([handles.edit1,
handles.edit2],'string'));
save(handles.datafile,'userdata')

% delete figure
delete(hObject);


By the way, if you want to combine GUI and SIMULINK, try to
use S-function.
Here is one iteractive simulation with GUI

http://www.mathworks.fr/matlabcentral/fileexchange/
loadFile.do?objectId=4652&objectType=file

HTH

Anh Huy Phan
RIKEN - BSI




Subject: GUI - reopening previous run

From: matt dash

Date: 9 Jan, 2008 18:31:03

Message: 7 of 10

As an alternate method for loading a previous run, you can
simply save the gui as a figure file once you've entered all
the variables. I use this method often.

Add "save" and "load" buttons to your gui, and give them
these callbacks:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function guisavestate(varargin)
handles=guidata(gcbo);

[name,path,index]=uiputfile({'*.gui','GUI Project
(*.gui)';'*.*','All Files (*.*)'},'Save state as:');

if ~isempty(name)
    mkdir([path,'\',name]);
    saveas(gcf,[path,'\',name,'\',name,'.gui'],'fig')
end

guidata(src,handles);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function guiloadstate(varargin)


[name,path,index]=uigetfile({'*.gui','GUI Project
(*.gui)';'*.*','All Files (*.*)'},'Choose state to load:');

if name~=0
    movefile([path,name],[path,name(1:end-3),'fig']);
    open([path,name(1:end-3),'fig']);
   
movefile([path,name(1:end-3),'fig'],[path,name(1:end-3),'gui'])

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
(There might be some minor errors in those, but you get the
idea)

All these are doing is saving the gui figure, and giving it
the extension .gui (so you dont confuse it with other .fig
files). Then when it loads the gui it temporarily changes
the extension back to .fig (since you can't load figures
without this extension)

Note that this won't fill the text boxes in your existing
gui, but will open a new figure containing the saved copy of
the gui. (So make sure that's not a problem for your setup
if you're used to using singleton gui's)

Subject: GUI - reopening previous run

From: Jason

Date: 10 Jan, 2008 12:17:02

Message: 8 of 10

"Anh Huy Phan" <phananhhuy@mathworks.com> wrote in message
<fm30k7$kve$1@fred.mathworks.com>...
> TO Jason:
>
> Basically, your purpose is to get values from the control
> objects ('edit text') in GUI, and assign them to variables
> in workspace. And you also want to save data in your GUI.
>
> In the OpeningFcn function, you check the existence of your
> data file and set value to your edit text. It looks like that
>
> function mygui_OpeningFcn(hObject, eventdata, handles)
> handles.output = hObject;
> handles.datafile =fullfile(pwd,'workspace.mat');
> data = [123,4567]; %default data edit1: 123, edit2:4567
> if exist(handles.datafile,'file')==2
> load(handles.datafile); %contains userdata variable
> data = userdata;
> end
> %set values to edittext objects
> set(handles.edit1,'string',num2str(data(1)));
> set(handles.edit2,'string',num2str(data(2)));
>
>
> % Update handles structure
> guidata(hObject, handles);
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
>
>
> Assume that 'mass' object is button to get data, here is its
> Callback function (you don't need to run other script file).
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> function mass_Callback(hObject, eventdata, handles)
> % get data
> data = str2double(get([handles.edit1,
> handles.edit2],'string'));
>
> % assign to variables in workspace
> assignin('base','var1',data(1));
> assignin('base','var2',data(2));
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> And you also need to code the CloseRequestFcn function of
> your figure.
>
>
> % --- Executes when user attempts to close figure1.
> function figure1_CloseRequestFcn(hObject, eventdata, handles)
>
> % update variables in workspace
> % mass_Callback(handles.mass, eventdata, handles)
>
> % get data and save
> userdata = str2double(get([handles.edit1,
> handles.edit2],'string'));
> save(handles.datafile,'userdata')
>
> % delete figure
> delete(hObject);
>
>
> By the way, if you want to combine GUI and SIMULINK, try to
> use S-function.
> Here is one iteractive simulation with GUI
>
> http://www.mathworks.fr/matlabcentral/fileexchange/
> loadFile.do?objectId=4652&objectType=file
>
> HTH
>
> Anh Huy Phan
> RIKEN - BSI
>
>
>
>

Anh Huy Phan,

Many thanks for your guidance. The GUI is now working as I
envisaged.

As I explained earlier I was executing the simulink model
from a separate .m file. I have since implemented your
method. It works fine - however the output variables from
simulink are not viewable in my workspace. I assume that I
should assign them to the workspace as we done previously?

Finally you mention the "CloseRequestFcn" Where do I find
this in GUIDE? I cannot seem to locate it. Or do I just
implement the text manually?

Thanks




% --- Executes on button press in execute.
function execute_Callback(hObject, eventdata, handles)
% hObject handle to execute (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

data = str2double(get([handles.mass,handles.Time],'string'));
assignin('base','mass',data(1));
assignin('base','Time',data(2));

sim('testmodels');
plot(tout,simout)
xlabel('Time')
ylabel('Displacement')
Title('Test')
grid on

Subject: GUI - reopening previous run

From: Anh Huy Phan

Date: 10 Jan, 2008 13:18:02

Message: 9 of 10

"Jason "
 
> method. It works fine - however the output variables from
> simulink are not viewable in my workspace.

Because 'testmodels' model was called in your function, so its
outputs (simout, tout, ...) exist only in the local function
scope. You can use assignin
    title('Test')
    grid on

    assignin('base','tout',tout);
    assignin('base','simout',simout);
    

>
> Finally you mention the "CloseRequestFcn" Where do I find
> this in GUIDE? I cannot seem to locate it. Or do I just
> implement the text manually?

Turn this function CloseRequestFcn on:
    - Right-click on your GUI in the GUIDE TOOL,
    - Choose 'ViewCallbacks' -> 'CloseRequestFcn'

HTH,

Anh Huy Phan
RIKEN - BSI

Subject: GUI - reopening previous run

From: Jason

Date: 11 Jan, 2008 08:27:02

Message: 10 of 10

"Anh Huy Phan" <phananhhuy@mathworks.com> wrote in message
<fm55ua$spr$1@fred.mathworks.com>...
> "Jason "
>
> > method. It works fine - however the output variables from
> > simulink are not viewable in my workspace.
>
> Because 'testmodels' model was called in your function, so
its
> outputs (simout, tout, ...) exist only in the local function
> scope. You can use assignin
> title('Test')
> grid on
>
> assignin('base','tout',tout);
> assignin('base','simout',simout);
>
>
> >
> > Finally you mention the "CloseRequestFcn" Where do I find
> > this in GUIDE? I cannot seem to locate it. Or do I just
> > implement the text manually?
>
> Turn this function CloseRequestFcn on:
> - Right-click on your GUI in the GUIDE TOOL,
> - Choose 'ViewCallbacks' -> 'CloseRequestFcn'
>
> HTH,
>
> Anh Huy Phan
> RIKEN - BSI

May thanks for the suggestions Anh Huy Phan. GUI working well!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
gui Jason 9 Jan, 2008 05:45:10
initial value Jason 9 Jan, 2008 05:45:10
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com