Defining GUI default value at object creation

3 views (last 30 days)
Remco1988
Remco1988 on 26 Oct 2015
Edited: Remco1988 on 26 Oct 2015
I couldn't immediatly find the following questions online:
General question 1) What determines the sequence execution of different object creations '_CreateFcn' or are they all run parallel?
General question 2) The 'OpeningFcn' runs after object creation. Is there a function that runs before object creation?
Specific question: My goal is to remember the property values of a GUI. So after properties have been changed (e.g by callbacks) and the GUI is closed, it should remember these properties after starting the GUI again. If it is the first time the GUI is started, or all appdata has been reset, it should use the default values.
At the moment (it works) I define these property values for each individual '_createFcn' (see example code), but for 20+ create functions it is very cumbersome. Is it possible to define my default appdata before object creation such that I don't need so many if loops (see example code 2)
example code 1 (current situation):
% --- Executes during object creation, after setting all properties.
function checkbox_res_mot_CreateFcn(hObject, eventdata, handles)
% hObject handle to checkbox_res_mot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if isappdata(0,'Simulation') %check if appdata exists
simulation = getappdata(0,'Simulation');
if any(strcmp(fieldnames(simulation),'residual_motions')) %check if variable exists
set(hObject,'Value',simulation.residual_motions); % stored value
else
set(hObject,'Value',0); % default value
end
else
set(hObject,'Value',0); % default value
end
simulation.residual_motions = get(hObject,'Value'); % create appdata
setappdata(0,'Simulation',simulation)
example code 2 (proposal):
%Executs before object creation
function default_values
if ~isappdata(0,'Simulation')
simulation.residual_motions = 0 % default values all in same function
setappdata(0,'Simulation',simulation) % create appdata
%%--- Executes during object creation, after setting all properties.
function checkbox_res_mot_CreateFcn(hObject, eventdata, handles)
% hObject handle to checkbox_res_mot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
simulation = getappdata(0,'Simulation'); % no need to check appdata as it has already been created
set(hObject,'Value',simulation.residual_motions); % residual_motions has been created in 'default value' code or is saved from last GUI execution

Answers (0)

Community Treasure Hunt

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

Start Hunting!