从另一个GUI调用Axes

时间:2015-02-21 18:44:06

标签: matlab image-processing matlab-guide

我创建了两个GUI。 在一个GUI中,我有axis1,在另一个GUI中,我有使用滑块模糊图像的功能。

GUI1 enter image description here

GUI2 GUI2

这是GUI2中的功能。

function gui_blurSlider_Callback(hObject, eventdata, handles)
% 
% 
% here global var img is a image used in GUI1 

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global var img slideval;
slideval=get(hObject,'Value');
fs = fspecial('gaussian', [3,3], slideval);
gblur = imfilter(img,fs,'replicate');

axes(handles.axes1);                   %error executing this line

imshow(gblur);

1 个答案:

答案 0 :(得分:1)

由于此命令可能会发生您的错误:

axes(handles.axes1)

指的是GUI 1的元素,而该行在GUI 2中执行,后者不识别来自GUI 1的变量,因为它们具有自己的工作空间。

您可以使用setappdata/getappdata解决此问题,或者在2个GUI之间共享数据。例如,在GUI 1 Opening_Fcn中,您可以编写如下内容:

setappdata(0,'hAxes1',handles.axes1)

handles.axes1存储在根目录中并使其在GUI 2中“可见”。然后在GUI 2中,从GUI 1中检索名为hAxes1的变量,该变量实际为handles.axes1

function gui_blurSlider_Callback(hObject, eventdata, handles)
% 
% 
% here global var img is a image used in GUI1 

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

%// NEW ==========================
    Axes1InGUI2 = getappdata(0,'hAxes1');
%// NEW ==========================

global var img slideval;
slideval=get(hObject,'Value');
fs = fspecial('gaussian', [3,3], slideval);
gblur = imfilter(img,fs,'replicate');

axes(Axes1InGUI2);  %// Change here

imshow(gblur); %// Or skip previous line and simply use imshow(gblur,'Parent',Axes1InGUI2)

上一个回答:

这是一个解决方案,其中滑块实际上位于轴1正下方的GUI 1中,因此您可以省去在两个GUI之间共享数据的麻烦。这是一个程序化的GUI,但其原理与GUIDE制作的GUI类似。如果你真的需要使用另一个GUI来完成任务,请询问。

以下是注释代码:

function GaussianSlider()
clear
clc
close all

%// Test image
handles.Image = imread('peppers.png');

%// Create GUI components
hFig = figure('Position',[500 500 500 500],'Units','pixels');

handles.axes1 = axes('Units','pixels','Position',[50 80 400 400]);
handles.slider = uicontrol('Style','slider','Position',[50 30 400 20],'Min',3,'Max',15,'Value',3);%// I commented this for the purpose of demonstration. 'Callback',@gaussian_blur(handles));

handles.Text1 = uicontrol('Style','text','Position',[80 70 70 20],'String','Slider Value');
handles.SValue = uicontrol('Style','text','Position',[160 70 70 20],'String','0');

%// Used to continously display the image as it's being changed
handles.Listener = addlistener(handles.slider,'Value','PostSet',@(s,e) gaussian_blur(handles));

%// The 'Parent' property is useful here.
imshow(handles.Image,'Parent',handles.axes1);

%// Update guidata.
guidata(hFig);

    %// Slider's listener object callback
    function gaussian_blur(handles)

        %// Get the slider's value
        slideval = round(get(handles.slider,'Value'));


        fs = fspecial('gaussian',slideval,slideval);

        handles.Image= imfilter(handles.Image,fs,'conv');

        %// Update text box
        set(handles.SValue,'String',num2str(slideval));

        imshow(handles.Image,'Parent',handles.axes1);

        guidata(hFig);

    end
end

当滑块为0时:

enter image description here

滑动之后:

enter image description here

相关问题