从外部函数到轴matlab GUI绘图

时间:2016-09-14 13:56:00

标签: matlab user-interface matlab-figure axes

我一直在尝试从外部函数到GUI内的轴获取绘图。我使用GUIDE。我尝试了多种方法,但我不断收到错误

Not enough input arguments.

Error in dummyGUI/plotButton_callback (line 19)
    set(hfigure,'CurrentAxes',handles.axes1)

现在我已经制作了一个虚拟GUI,并且在命令窗口中创建了绘图,但我无法摆脱错误。

我的代码非常简单,如下所示: 函数dummyGUI

f = figure('Name','Name1','Tag','Name1','Units','Pixels','Position',[50 50 1000 600]);

   plotButton = uicontrol('Style', 'pushbutton',...
                         'Parent', f,...
                         'String', 'plot',...
                         'Units', 'pixels', 'Position', [100 400 100 20],...
                         'Callback',@plotButton_callback);

    axes1 = axes('Parent', f,... 
                    'Units', 'pixels', 'Position', [50 50 500 300]);



    function plotButton_callback(hObject, eventdata, handles)
        hfigure = getappdata(0,'hfigure');
        axes1 = getappdata(0,'axes1')

        set(hfigure,'CurrentAxes',handles.axes1)

    end
end

我在命令窗口中使用的代码是:

x = 1:100;
plot(x,x.^2);
hfigure = gcf;
hfigure = setappdata('0','hfigure')

显然有一些缺失,但我不知道是什么。

非常感谢提前。

罗马诺

2 个答案:

答案 0 :(得分:0)

您的代码的直接问题是,由于您没有使用GUIDE,因此只有两个输入提供给回调函数:

  1. 触发回调的对象
  2. 回调事件数据
  3. 未提供handles输入,因此当您尝试访问它时,MATLAB会发出有关输入参数数量不正确的错误。

    你应该自己明确地将必要的句柄传递给回调。

    set(plotButton, 'Callback', @(src ,evnt)plotButton_callback(src, evnt, axes1))
    
    function plotButtonCallback(hObject, eventdata, axes1)
        hfigure = ancestor(hObject, 'figure');
        set(hfigure, 'CurrentAxes', axes1)
    end
    

    或者由于plotButtonCallback是主要功能的子功能,因此您可以访问父功能的axes1f变量

    function plotButtonCallback(hObject, eventdata)
        set(f, 'CurrentAxes', axes1)
    end
    

    其他问题

    当您致电setappdata时,您正在传递字符串 '0'而不是图形根对象0。此外,您需要向setappdata提供第三个​​输入以实际提供该值。

    setappdata(0, 'hfigure', hfigure)
    

    通常,将内容保存在根(0)对象的appdata中是一个坏主意,因为如果你有两个GUI运行实例,它们会相互干扰。

答案 1 :(得分:0)

我找到了一种方法,也许它不是正确的但是有效。除此之外,我还发现了为什么我没有得到任何情节和任何错误信息。不知何故,变量没有正确传递,而且只是空的。 所以变量存在,但它没有包含任何内容,因此没有错误信息,也没有情节。

我现在这样做的方式是。

function plotButton_callback(hObject,eventdata,handles)
    set(mainScreen,'CurrentAxes',plotFig);
    [struct] = compare_cycle(var1,var2);
end

和compare_cycle.m

function [struct] = compare_cycle(var1,x)
     struct = plot(xaxis,yaxis,...)
end

请不要关心所有变量的名称,因为这只是一个试用版。

感谢您的帮助Suever。

相关问题