MATLAB图形用户界面中通用变量的生成
我对MATLAB有点陌生,我正在为一个学校项目做一些实验 我想要的是一个有3个按钮的GUI,当您按下前两个按钮中的任何一个按钮时,它会在一个变量上加上一个(每个按钮对应一个变量),当您按下第三个按钮时,它会处理前两个按钮中的变量 我使用“向导”,拖放按钮,然后修改函数 但我意识到我的变量只存在于按钮的函数中,因此如果我初始化它们,每次按下按钮时它们都会重新启动,而且我的第三个按钮也无法知道前两个的值 有没有办法使这些变量始终存在?或者将它们从一个函数传递到另一个函数 我的代码它只是由“guide”生成的自动代码,v1=v1+1;在第一个按钮回调函数中,v2=v2+1在第二个按钮回调函数中,disp(v1)disp(v2)在第三个按钮回调函数中 我希望你明白我的意思,我不是以英语为母语的人,所以MATLAB图形用户界面中通用变量的生成,matlab,variables,button,user-interface,Matlab,Variables,Button,User Interface,我对MATLAB有点陌生,我正在为一个学校项目做一些实验 我想要的是一个有3个按钮的GUI,当您按下前两个按钮中的任何一个按钮时,它会在一个变量上加上一个(每个按钮对应一个变量),当您按下第三个按钮时,它会处理前两个按钮中的变量 我使用“向导”,拖放按钮,然后修改函数 但我意识到我的变量只存在于按钮的函数中,因此如果我初始化它们,每次按下按钮时它们都会重新启动,而且我的第三个按钮也无法知道前两个的值 有没有办法使这些变量始终存在?或者将它们从一个函数传递到另一个函数 我的代码它只是由“guide
无论如何,非常感谢,希望这是一个容易解决的问题。以下不是大型复杂程序的最佳实践,但对于一些简单的操作,如您正在尝试的操作,听起来全局变量将是完美的。假设
X
、Y
和Z
是要在函数之间共享的变量。在使用它们的每个函数的开头添加以下内容,它们都可以访问相同的值
global X Y Z
您有几个选择:
全局变量
但是使用全局变量不是一种好的做法:请参阅,或
setappdata
/getappdata
函数存储变量(这是最简单的一个)句柄
结构(此结构更复杂)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);