Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MATLAB图形用户界面中通用变量的生成_Matlab_Variables_Button_User Interface - Fatal编程技术网

MATLAB图形用户界面中通用变量的生成

MATLAB图形用户界面中通用变量的生成,matlab,variables,button,user-interface,Matlab,Variables,Button,User Interface,我对MATLAB有点陌生,我正在为一个学校项目做一些实验 我想要的是一个有3个按钮的GUI,当您按下前两个按钮中的任何一个按钮时,它会在一个变量上加上一个(每个按钮对应一个变量),当您按下第三个按钮时,它会处理前两个按钮中的变量 我使用“向导”,拖放按钮,然后修改函数 但我意识到我的变量只存在于按钮的函数中,因此如果我初始化它们,每次按下按钮时它们都会重新启动,而且我的第三个按钮也无法知道前两个的值 有没有办法使这些变量始终存在?或者将它们从一个函数传递到另一个函数 我的代码它只是由“guide

我对MATLAB有点陌生,我正在为一个学校项目做一些实验

我想要的是一个有3个按钮的GUI,当您按下前两个按钮中的任何一个按钮时,它会在一个变量上加上一个(每个按钮对应一个变量),当您按下第三个按钮时,它会处理前两个按钮中的变量

我使用“向导”,拖放按钮,然后修改函数

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

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

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

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


无论如何,非常感谢,希望这是一个容易解决的问题。

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

global X Y Z

您有几个选择:

  • 使用nhowe建议的
    全局变量
    但是使用全局变量不是一种好的做法:请参阅,或
  • 使用
    setappdata
    /
    getappdata
    函数存储变量(这是最简单的一个)
  • 了解如何使用和正确更新指南中创建的GUI控件的每个回调函数中出现的
    句柄
    结构(此结构更复杂)
  • 下面是案例#3的*.m文件示例。大部分指南生成的代码被删除,只显示与变量相关的内容。基本上,您必须更新每个回调函数中的
    handles
    结构,该回调函数使用
    guidata(hObject,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);