我喜歡在創建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