我喜欢在创建GUI对象之前定义变量(例如Appdata
)。我无法立刻找到以下问题在线:在创建对象之前定义MATLAB GUI默认值
一般问题1)什么决定了不同的对象创建功能
_CreateFcn
执行的顺序或者是他们全部被并联运行?一般问题2)
OpeningFcn
运行后对象创建。是否有在对象创建之前运行的函数?具体问题:我的目标是记住GUI的属性值。因此,在属性更改后(例如通过回调)并关闭GUI后,应在再次启动GUI后记住这些属性。如果这是GUI第一次启动,或者所有appdata已被重置,则应使用默认值。
目前(它的工作),我定义这些属性值对每个个体_createFcn
(见示例代码),但对于20+创造功能是非常麻烦。是否有可能在创建对象之前定义我的默认appdata,这样我不需要太多的循环? (见示例代码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
对不起,我晚了反应,但感谢您的反馈! 您提到我可以使用.mat'文件或工作区来存储我的GUI会话。但是,这并不是问题,因为我将所有内容都存储在'appdata(0)'中。 我想在**对象创建之前加载此会话**的原因是因为每个对象'CreateFcn'都使用此信息。 对一般问题你的2个答案让我意识到这将是更可靠的,从'CreateFcn'删除所有代码和'OpeningFcn'中定义的一切。我需要弄清楚如何首先使用'handles',然后我会在这里更新我的工作! – Remco
除非我误解了,关闭GUI后'appdata'将会丢失。它意味着在GUI活动时在回调之间共享数据。如果你想要在GUI执行期间生成的信息,你需要永久的手段,比如将数据保存在工作区中的文件或变量中。 – user3667217
在我的情况下,只要Matlab本身打开,就会存储'appdata'。所以每次Matlab开始的时候都只能使用默认值,这对我来说很好。也许我没有使用'appdata',因为它'应该'被使用。它似乎很容易将所有我的(全局)变量存储在其中。 – Remco