2013-05-09 85 views
0

我對MATLAB很陌生,我正在爲一個學校項目做一些實驗。在MATLAB中製作通用變量GUI

我要的是有3個按鈕的GUI,當您按下任一前兩個,加起來比一的變量(每個按鈕一個變量),而當你按下第三個按鈕,它做一些事情與前兩個按鈕的變量。

我用「指南」,並拖放按鈕,然後我修改了功能。

但我意識到我的變量只存在於按鈕的函數內部,所以如果我初始化它們,每次按下按鈕時它們都會重新啓動,而且我的第三個按鈕也無法知道第一個按鈕的值二。

有沒有辦法讓這個變量總是存在?或者將它們從一個函數傳遞給另一個?

我的代碼只是由「guide」生成的自動代碼,v1 = v1 + 1;在第一個按鈕回調函數中,v2 = v2 + 1在第二個中,disp(v1)disp(v2)在第三個中。

我希望你明白我的意思,我不是以英語爲母語所以......

無論如何,非常感謝,希望它的東西很容易解決。

回答

0

以下不是針對大型複雜程序的最佳做法,但對於您想要做的事情那樣簡單,聽起來像全局變量將是完美的。假設X,YZ是您想要在功能之間共享的變量。在每個使用它們的函數的開始處添加以下內容,並且它們都可以訪問相同的值。

global X Y Z 
2

您有幾種選擇:

  1. 使用global變量nhowe建議。
    但使用全局變量是不是一個好的做法:看Top 10 MATLAB code practices that make me cry,或Wikipedia article
  2. 使用setappdata/getappdata功能來存儲您的變量(這是簡單的一個)
  3. 學習如何使用和正確更新出現的handles結構在GUIDE中創建的GUI控件的每個回調函數中(這個更復雜)。

以下是案例#3的* .m文件的示例。大多數GUIDE生成的代碼已被刪除,只顯示與變量相關的內容。基本上,您必須更新每個回調函數中的handles結構,並使用guidata(hObject, handles);行對其進行一些更改。在此後所有後續回調將看到更新的handles結構。

function varargout = GUIProgramWithVariables(varargin) 
    % Here goes some comment from GUIDE 
    % Begin initialization code - DO NOT EDIT 
    % . . .    actual code skipped 
    % End initialization code - DO NOT EDIT 

% --- Executes just before GUIProgramWithVariables is made visible. 
function GUIProgramWithVariables_OpeningFcn(hObject, eventdata, handles, varargin) 
    % This function has no output args, see OutputFcn. 
    % hObject handle to figure 
    % eventdata reserved - to be defined in a future version of MATLAB 
    % handles structure with handles and user data (see GUIDATA) 
    % varargin command line arguments to GUIProgramWithVariables (see VARARGIN) 
    % Choose default command line output for GUIProgramWithVariables 
    handles.output = hObject; 
    % Here your code starts. It should be at the end of OpeningFcn 
    % Add your fields to handles structure 
    handles.C1 = 1; 
    handles.C2 = 2; 
    handles.C3 = 3; 
    % this updates modified handles structure 
    % so all subsequent call-backs will see the changes 
    guidata(hObject, handles); 

% --- Executes on button press in Button1 
function Button1_Callback(hObject, eventdata, handles) 
    % hObject handle to BrowseButton (see GCBO) 
    % eventdata reserved - to be defined in a future version of MATLAB 
    % handles structure with handles and user data (see GUIDATA) 
    % Here we do the magic with Button1 
    handles.C1 = handles.C1 + 1; 
    % this updates modified handles structure 
    % so all subsequent call-backs will see the changes 
    guidata(hObject, handles); 

% --- Executes on button press in Button2 
function Button1_Callback(hObject, eventdata, handles) 
    % hObject handle to BrowseButton (see GCBO) 
    % eventdata reserved - to be defined in a future version of MATLAB 
    % handles structure with handles and user data (see GUIDATA) 
    % Here we do the magic with Button2 
    handles.C2 = handles.C2 + 1; 
    % this updates modified handles structure 
    % so all subsequent call-backs will see the changes 
    guidata(hObject, handles); 

% --- Executes on button press in Button3 
function Button3_Callback(hObject, eventdata, handles) 
    % hObject handle to BrowseButton (see GCBO) 
    % eventdata reserved - to be defined in a future version of MATLAB 
    % handles structure with handles and user data (see GUIDATA) 
    % Here we do the magic with Button3 
    handles.C3 = handles.C1 + handles.C2; 
    % this updates modified handles structure 
    % so all subsequent call-backs will see the changes 
    guidata(hObject, handles);