【发布时间】:2016-02-05 05:26:21
【问题描述】:
我喜欢在创建 GUI 对象之前定义变量(例如Appdata)。我在网上找不到以下问题:
一般问题 1) 是什么决定了不同对象创建函数
_CreateFcn的执行顺序,还是它们都并行运行?一般问题 2)
OpeningFcn在对象创建后 运行。是否有在对象创建之前运行的函数?具体问题:我的目标是记住 GUI 的属性值。因此,在更改属性(例如通过回调)并关闭 GUI 后,它应该在再次启动 GUI 后记住这些属性。如果是第一次启动 GUI,或者如果所有 appdata 都已重置,则应使用默认值。
目前(它有效)我为每个 _createFcn 定义了这些属性值(参见示例代码),但是对于 20 多个创建函数,它非常麻烦。是否可以在创建对象之前定义我的默认 appdata,这样我就不需要那么多 if 循环? (见示例代码2)
示例代码1(当前情况):
% --- 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)
示例代码2(提案):
%Executs before object creation
function default_values
if ~isappdata(0,'Simulation')
simulation.residual_motions = 0 % default values all in same function (only 1 variable shown as example)
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
【问题讨论】:
标签: matlab user-interface matlab-guide matlab-deployment appdata