我是MATLAB的新手,我正在为一个学校项目做一些实验。
我想要的是一个带有3个按钮的GUI,当你按下前两个按钮时,它在变量上加一个(每个按钮一个变量),当你按下第三个按钮时,它会做一些事情来自前两个按钮的变量。
我使用“指南”并拖放按钮,然后修改了功能。
但是我意识到我的变量只存在于按钮的函数内部,所以如果我初始化它们会在每次按下按钮时重新启动,而且我的第三个按钮也无法知道前两个的值
有没有办法让这些变量始终存在?或者将它们从一个函数传递给另一个函数?
我的代码只是由“guide”生成的自动代码,其中v1 = v1 + 1;在第一个按钮回调函数中,v2 =第二个中的v2 + 1,第三个中的disp(v1)disp(v2)。
我希望你明白我的意思,我不是英语母语人士......
无论如何,非常感谢,希望这很容易解决。
答案 0 :(得分:2)
您有几种选择:
global
变量作为 nhowe 建议。 setappdata
/ getappdata
函数存储变量(这是更简单的函数)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);
答案 1 :(得分:0)
以下不是大型复杂程序的最佳实践,但对于像您尝试做的那样简单的事情听起来像全局变量将是完美的。假设X
,Y
和Z
是要在函数之间共享的变量。在每个使用它们的函数的开头添加以下内容,它们都可以访问相同的值。
global X Y Z