从另一个 GUI 调用 Axes

Call Axes from another GUI

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

GUI1

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);

你的错误很可能是因为这个命令:

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 中使其成为 'visible'。然后在 GUI 2 中,检索名为 hAxes1 的变量,它实际上是 handles.axes1 来自 GUI 1:

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 中,因此您可以省去在 2 个 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时:

滑动后: