【问题标题】:Making universal variables in MATLAB GUI在 MATLAB GUI 中制作通用变量
【发布时间】:2013-05-04 10:40:45
【问题描述】:

我是 MATLAB 的新手,我正在为一个学校项目做一些实验。

我想要的是一个带有 3 个按钮的 GUI,当你按下前两个按钮中的任何一个时,它会在一个变量上加起来一个(每个按钮一个变量),当你按下第三个按钮时,它会做一些事情前两个按钮的变量。

我使用“指南”并拖放按钮,然后修改功能。

但是我意识到我的变量只存在于按钮的函数中,所以如果我初始化它们,每次按下按钮时它们都会重新启动,而且我的第三个按钮也无法知道前两个的值.

有没有办法让这些变量始终存在?或者将它们从一个函数传递给另一个函数?

我的代码它只是“指南”生成的自动代码,v1 = v1+1;在第一个按钮回调函数中,第二个中为 v2 = v2+1,第三个中为 disp(v1) disp(v2)。

我希望你明白我的意思,我不是以英语为母语的人所以......

无论如何,非常感谢,希望它很容易解决。

【问题讨论】:

    标签: matlab variables button user-interface


    【解决方案1】:

    您有多种选择:

    1. 按照nhowe 的建议使用global 变量。
      但是使用全局变量不是一个好习惯:见Top 10 MATLAB code practices that make me cry,或Wikipedia article
    2. 使用setappdata / getappdata 函数来存储变量(这个比较简单)
    3. 了解如何使用和正确更新在 GUIDE 中创建的 GUI 控件的每个回调函数中出现的 handles 结构(这个更复杂)。

    这里是案例#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); 
    

    【讨论】:

      【解决方案2】:

      以下不是大型复杂程序的最佳实践,但对于像您尝试做的简单的事情,听起来全局变量是完美的。假设X、Y 和Z 是您要在函数之间共享的变量。在使用它们的每个函数的开头添加以下内容,它们都可以访问相同的值。

      global X Y Z
      

      【讨论】:

        猜你喜欢
        • 2015-03-29
        • 1970-01-01
        • 2013-05-06
        • 2017-05-16
        • 2013-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-06
        相关资源
        最近更新 更多