Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.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
如何在MatlabGUI中实现用户功能?_Matlab_User Interface - Fatal编程技术网

如何在MatlabGUI中实现用户功能?

如何在MatlabGUI中实现用户功能?,matlab,user-interface,Matlab,User Interface,假设我有两个函数: 1.[AB 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 versi

假设我有两个函数:
1.[AB 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中定义了a。

谁能告诉我我做错了什么,或者如何正确地实现这些功能

是的,您在
按钮1\u回调中定义了一个按钮,并且只在那里定义。在
pushbutton2\u Callback
中,您处于不同的工作区中,即使它们位于同一文件中

资料来源:


解决方案是,因为看起来您正在使用guide,所以要使用handles结构:

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
该指南通过handles结构处理所有数据,默认情况下,几乎在每个回调中都会给出该指南。因此,您可以在第二个按钮的回调中使用它:

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

您得到错误是因为您所做的每个函数(
function1()
function2(a)
)的工作区不与其他函数共享。为了避免这个问题,您有几种选择,其中2种是:

1) 使用GUI的句柄结构存储变量,并在回调和函数之间轻松共享它们(检查信息)

2) 使用和将应用程序定义的数据与GUI图形相关联。我将让您阅读有关这些数据的文档,但这里有两个使用每个方法的示例。我简化了
function1()
,使演示只有一个输出,但相同的原则适用于更多的输出参数

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

就这样。玩得高兴如果有什么不清楚的,请问我

非常感谢,我现在完全明白了。我想问另一件事,我为gui的背景图像制作了一个轴。现在,当我按下子图2图像的按钮时,它不会弹出另一个窗口,而是在背景图像的轴上显示图像。知道为什么吗?不客气。如果有帮助,请单击答案旁边的绿色复选标记,将答案标记为已接受。至于您的问题,您可以通过在子地块之前调用
figure()
来创建新地物,或者使用
imshow
Parent
属性来选择轴。查看文档以了解更多详细信息。