How do i save the state of my GUI and load the saved state again?

2 views (last 30 days)
I am making a program to book airline tickets. Each radio button represents a seat in the plane. I changed the color of each button when selected to indicate that the seat is occupied. This will show the next person who runs the program that he cannot select that seat............. E.g. of seat names (LW1, LW2, LW3 etc.......)
function uipanel4_SelectionChangeFcn(hObject, eventdata, handles)
A = get(hObject,'String');
switch A
case 'LW1'
if (get(hObject,'Value')== get(hObject,'Max'))
set(hObject, 'BackgroundColor','red')
end
case 'LW2'
if (get(hObject,'Value')== get(hObject,'Max'))
set(hObject, 'BackgroundColor','red')
end
end
How can I save the state of the buttons so that a seat cannot be selected twice?
I made an "ok" push button to carry out the save function and it didn't work. This is the code:
function ok_KeyPressFcn(hObject, eventdata, handles)
saveState(handles)
function saveState(handles)
state.radioVal = get(handles.radiobutton2,'BackgroundColor');
save ('state.mat', 'state')
function loadState(handles)
filename = 'state.mat'
if exist(filename)
load(filename)
set(handles.radiobutton2,'BackgroundColor', state.radioVal);
end

Accepted Answer

Geoff Hayes
Geoff Hayes on 14 Nov 2014
Rushane - what do you mean by and it didn't work? It is sometimes helpful to provide more information. Is there an error, and if so, what is it?
If I run your code, then an error does appear as
SWITCH expression must be a scalar or string constant.
Error in untitled1>uipanel4_SelectionChangeFcn
This is because the data type for
A = get(hObject,'String');
is cell. You must convert the string property of the radio button to a character array as
A = char(get(hObject,'String'));
then the switch expression should be fine. The code to check for whether the radio button has been selected (or not) can be simplified from
if (get(hObject,'Value')== get(hObject,'Max'))
to
if get(hObject,'Value')
The rest should work without error provided that loadState is called from your OpeningFcn.
One thing that you may want to consider is a way to generalize the state object so that you can store the data for each radio button in a field specific to that radio button. Try using its tag as follows
function saveState(handles)
state = [];
% get all fields from handles
guiFields = fields(handles);
% iterate through each field
for k=1:length(guiFields)
obj = handles.(guiFields{k});
% if the object is a scalar AND a graphics handle and has a field
% called Style
if isscalar(obj) && ishghandle(obj) && isfield(get(obj),'Style')
style = get(obj,'Style');
if strcmpi(style,'radiobutton')
% style is radiobutton so save its background color to the
% state structure
state.(guiFields{k}) = get(obj,'BackgroundColor');
end
end
end
% write the state to file
save('state.mat','state');
The loadState function would need to change to something like
function loadState(handles)
filename = 'state.mat';
if exist(filename,'file')
load(filename);
% get all fields from state
stateFields = fields(state);
% iterate through each field
for k=1:length(stateFields)
set(handles.(stateFields{k}),'BackgroundColor', state.(stateFields{k}));
end
end
  3 Comments
Geoff Hayes
Geoff Hayes on 23 Nov 2014
Edited: Geoff Hayes on 23 Nov 2014
Rushane - put a breakpoint a the line
set(handles.(stateFields{k}),'BackgroundColor',state.(stateFields{k}));
and then run launch your GUI. When the code pauses at the breakpoint, look at the state structure and the state.(stateFields{k}) value. What is it, and does it make sense?
As for making a radio button invisible after it has been selected, look at the Visible property of the radio button and try changing its value from on to off.

Sign in to comment.

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!