2013-05-09 100 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);