Matlab GUI 子窗口怎么使用父窗口的控件

我在父窗口GUI建了一个菜单打开新的子窗口GUI_1,然后在GUI_1中输入数据处理图像绘图到父窗口GUI的axes2中,参考了网上的不同窗口数据传递的代码,一直出现【引用了不存在的字段 'axes2'】的问题
父窗口GUI部分代码

function Untitled_32_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_32 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
h=GUI_1(handles.figure1);
GUI_1Handle=guihandles(h);
handles.GUI_1handles=GUI_1Handle;
guidata(hObject,handles);

set(GUI_1,'Visible','on');

子窗口GUI_1部分代码

function GUI_1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to GUI_1 (see VARARGIN)
h=varargin{1};
handles.GUIhandle=guihandles(h);
guidata(hObject,handles);
% Choose default command line output for GUI_1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes GUI_1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

GUIHandle=handles.GUIhandle;
axes(GUIHandle.axes2);
imshow(uint8(res));

我可以通过以下代码实现父窗口和子窗口之间的控件传递:

在父窗口的菜单回调函数中,添加以下代码实现打开子窗口 GUI_1:

function menu_open_GUI_1_Callback(hObject, eventdata, handles)
    % 打开子窗口 GUI_1
    GUI_1
end

在子窗口 GUI_1 的图像处理回调函数中,将处理后的图像传递回父窗口的 axes2 控件中:

function process_button_Callback(hObject, eventdata, handles)
    % 图像处理
    processed_image = process_image(handles.original_image); % 假设这里是图像处理函数
    % 将处理后的图像显示在父窗口的 axes2 控件中
    parent_handles = guidata(handles.parent_GUI_handle); % 获取父窗口的 handles
    axes(parent_handles.axes2);
    imshow(processed_image)
end

这里需要获取父窗口的 handles,可以通过子窗口的 handles.parent_GUI_handle 属性获取,这个属性可以在子窗口 GUI_1 中的 OpeningFcn 回调函数中设置:

function GUI_1_OpeningFcn(hObject, eventdata, handles, varargin)
    % 设置子窗口的 handles.parent_GUI_handle 属性为父窗口的 handles
    handles.parent_GUI_handle = varargin{1};
    guidata(hObject, handles);
    % 其他初始化代码省略
end

需要在父窗口菜单回调函数中传递父窗口的 handles 变量到子窗口 GUI_1 中,也就是第一个代码段中调用的语句中。例如:

function menu_open_GUI_1_Callback(hObject, eventdata, handles)
    % 打开子窗口 GUI_1
    GUI_1(handles)
end

最后附上完整代码示例:

父窗口代码(main_GUI.m):

function varargout = main_GUI(varargin)
% MAIN_GUI MATLAB code for main_GUI.fig
%      MAIN_GUI, by itself, creates a new MAIN_GUI or raises the existing
%      singleton*.
%
%      H = MAIN_GUI returns the handle to a new MAIN_GUI or the handle to
%      the existing singleton*.
%
%      MAIN_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MAIN_GUI.M with the given input arguments.
%
%      MAIN_GUI('Property','Value',...) creates a new MAIN_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before main_GUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to main_GUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help main_GUI

% Last Modified by GUIDE v2.5 24-Jul-2021 19:39:39

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @main_GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @main_GUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before main_GUI is made visible.
function main_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to main_GUI (see VARARGIN)

% Choose default command line output for main_GUI
handles.output = hObject;

% 设置父窗口的 handles
handles.parent_handles = handles;
guidata(hObject, handles);

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes main_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = main_GUI_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in open_GUI_1_button.
function open_GUI_1_button_Callback(hObject, eventdata, handles)
% 打开子窗口 GUI_1
GUI_1(handles)


% --- Executes on button press in test_button.
function test_button_Callback(hObject, eventdata, handles)
% 测试按钮
disp(handles); % 输出父窗口的 handles

子窗口代码(GUI_1.m):

function varargout = GUI_1(varargin)
% GUI_1 MATLAB code for GUI_1.fig
%      GUI_1, by itself, creates a new GUI_1 or raises the existing
%      singleton*.
%
%      H = GUI_1 returns the handle to a new GUI_1 or the handle to
%      the existing singleton*.
%
%      GUI_1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_1.M with the given input arguments.
%
%      GUI_1('Property','Value',...) creates a new GUI_1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before GUI_1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to GUI_1_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help GUI_1

% Last Modified by GUIDE v2.5 24-Jul-2021 19:48:49

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI_1_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI_1_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before GUI_1 is made visible.
function GUI_1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to GUI_1 (see VARARGIN)

% Choose default command line output for GUI_1
handles.output = hObject;

% 设置父窗口的 handles
handles.parent_handles = varargin{1}.parent_handles;
guidata(hObject, handles);

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes GUI_1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = GUI_1_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in process_button.
function process_button_Callback(hObject, eventdata, handles)
% 图像处理
processed_image = process_image(handles.original_image); % 假设这里是图像处理函数
% 将处理后的图像显示在父窗口的 axes2 控件中
parent_handles = guidata(handles.parent_handles.figure1); % 获取父窗口的 handles
axes(parent_handles.axes2);
imshow(processed_image)


% --- Executes on button press in open_image_button.
function open_image_button_Callback(hObject, eventdata, handles)
% 选择图片文件
[file, path] = uigetfile({'*.jpg;*.png;*.bmp', 'Images (*.jpg, *.png, *.bmp)'});
if file == 0
    return % 用户取消选择,不执行后面的代码
end
% 读取图片
image_data = imread(fullfile(path, file));
% 在 edit 中显示文件路径和文件名
set(handles.file_path_edit, 'String', fullfile(path, file));
% 在 axes 中显示图片
axes(handles.image_axes);
imshow(image_data);
% 保存原始图像数据到 handles.original_image 变量中
handles.original_image = image_data;
guidata(hObject, handles);