让我们说我有2个功能:
1. [a b c] = func1()
2. [d] = func2(a)
我用2个按钮做了一个gui,并在回调函数之后复制粘贴了两个函数调用:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[a b c]=func1()
和第二个按钮:
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[d]=func2(a)
但是当我运行gui时,它会给出未知函数变量a的错误,但是我在func1中定义了一个 任何人都可以告诉我我做错了什么或如何正确实现这些功能。
答案 0 :(得分:2)
您收到错误,因为您所做的每个功能的工作区(function1()
和function2(a)
)都不与其他功能共享。为了避免这个问题,你有几个选择,其中2个如下:
1)使用GUI的句柄结构来存储变量,并在回调和函数之间轻松共享它们(检查信息here)
2)使用setappdata和getappdata将应用程序定义的数据与GUI图形相关联。我会让你阅读有关这些的文档,但这里有两个使用每种方法的例子。我将function1()
简化为只有1个输出用于演示,但相同的原则适用于更多的输出参数。
1)处理结构
由于您使用GUIDE来创建GUI,请注意可以从GUI中的任何回调方便地访问句柄结构(它始终作为输入参数传递)。因此,您不需要在开头使用这行代码:
handles = guidata(hfigure);
使用GUI来查看它的行为。
以下是代码:
function TestFuncGUI
clc
clear all
hfigure = figure('Position',[100 100 200 100],'Units','normalized');
handles.Button1= uicontrol('Style','push','String','1','Position',[40 50 50 30],'Callback',@(s,e) btn1_callback);
handles.Button2= uicontrol('Style','push','String','2','Position',[100 50 50 30],'Callback',@(s,e) btn2_callback);
guidata(hfigure,handles);
%// Callback for button 1
function btn1_callback
handles = guidata(hfigure);
%// Assign output of function1 to handles.a variable.
handles.a = function1();
%// Update handles structure.
guidata(hfigure,handles);
end
%// function1
function a = function1()
handles = guidata(hfigure);
%// Define a.
a = magic(5);
guidata(hfigure,handles);
end
%// Callback for button 2. Call function2 and assign output to handles.d
function btn2_callback
handles = guidata(hfigure);
%// IMPORTANT. Call function2 with handles.a
handles.d = function2(handles.a);
guidata(hfigure,handles);
end
%// function2
function d = function2(a)
handles = guidata(hfigure);
%// Dummy calculation and display result.
d = a+10;
disp(d)
guidata(hfigure,handles);
end
end
2)setappdata和getappdata
function TestFuncGUI2
clc
clear all
hfigure = figure('Position',[100 100 200 100],'Units','normalized');
handles.Button1= uicontrol('Style','push','String','1','Position',[40 50 50 30],'Callback',@(s,e) btn1_callback);
handles.Button2= uicontrol('Style','push','String','2','Position',[100 50 50 30],'Callback',@(s,e) btn2_callback);
%// Callback for button 1
function btn1_callback
a = function1();
%// Store "a" in the application data
setappdata(hfigure,'a',a);
end
%// function1
function a = function1()
%// Define a.
a = magic(5);
setappdata(hfigure,'a',a);
end
%// Callback for button 2. Call function2 and assign output to handles.d
function btn2_callback
%// Retrieve "a" with getappdata.
a = getappdata(hfigure,'a');
%// Call function "d" with a.
d = function2(a);
end
%// function2
function d = function2(a)
%// Dummy calculation and display result.
d = a+10;
disp(d)
guidata(hfigure,handles);
end
end
就是这样。玩得开心!如果有什么不清楚请问我!
答案 1 :(得分:0)
是的,您在pushbutton1_Callback
中定义了一个,但仅在那里。在pushbutton2_Callback
中,即使它们位于同一个文件中,您也处于不同的工作空间中。
来源:http://ch.mathworks.com/help/matlab/matlab_prog/base-and-function-workspaces.html
解决方案是,因为看起来你正在使用指南,所以使用句柄struct:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[a b c]=func1()
handles.a=a; % write your Variable into the handles Struct
guidata(hObject,handles) % save the handles struct itselfe
指南处理handle-Struct上的所有数据,默认情况下几乎每个回调都会给出它。所以你可以在第二个按钮的回调中使用它:
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[d]=func2(handles.a) % now get the Variable a from the handles struct