matlab 不同GUI之间的交互/不同GUI之间的数据传递

matlab 不同GUI之间实现交互:

功能介绍:

当在主界面单击列表框中的选项时,次界面的输入框中自动显示主界面中的选项。按钮就不介绍啦,想想不是关键的东西,想要明白按钮的功能也十分简单,下面会把代码用大白话讲清楚!
在这里插入图片描述

方法一:采用global函数传递数据

众所周知,global函数是一个申明全局变量的函数,在回调函数之间的数据传递和不同gui之间的数据传递起到很关键的作用。因为用global声明变量以后,变量将被保存在工作空间中。

首先创建两个gui界面,主界面为test.fig 次界面为test02.fig。然后做好相应的布局。在主界面中添加列表框,并且添加几个选项,作为测试用。在次界面中添加按钮和输入框。

如果连上面那步都不会,那就真的应该抽自己几个大嘴巴子哈哈哈哈哈哈

废话不说啦直接上代码:

test代码:

function varargout = test(varargin)
% TEST MATLAB code for test.fig
%      TEST, by itself, creates a new TEST or raises the existing
%      singleton*.
%
%      H = TEST returns the handle to a new TEST or the handle to
%      the existing singleton*.
%
%      TEST('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEST.M with the given input arguments.
%
%      TEST('Property','Value',...) creates a new TEST or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before test_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to test_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 test

% Last Modified by GUIDE v2.5 12-Sep-2021 15:30:55

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @test_OpeningFcn, ...
                   'gui_OutputFcn',  @test_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 test is made visible.
function test_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 test (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes test wait for user response (see UIRESUME)
% uiwait(handles.mainfig);


% --- Outputs from this function are returned to the command line.
function varargout = test_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 selection change in listbox1.
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%列表框的回调函数,点击列表框里面的内容时会调用此函数%%%%%%%%%%%%%%%
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1

global str %%声明一个全局变量,是为了给次界面传递数据
if isequal(get(gcf,'SelectionType'),'normal')%%%get(gcf,'SelectionType')是获取鼠标点击类型,normal代表鼠标单击
%% 如果鼠标双击的话就是open,
%%  isequal(get(gcf,'SelectionType'),'normal')意思是:获取鼠标单击
    n = get(hObject,'value');% n代表鼠标单击的是列表框中的第几个选项,返回值n是一个int类型的值
    str_all = get(hObject,'string')% 获取列表框中的所有数据
    %%
    {'张三' }
    {'李四' }
    {'王五' }
    {'赵六' }
    {'liqi' }
    {'王八' }
    {'杜九' }
    {'陈十' }
    {'钱十一'}
    {'许十二'}
    {'贾十三'}
    {'白十四'}
	%%
    str = str_all{n};% 获取到你点击的是列表中的哪一个值,这里是值,而n是获取到点击的是第几个,返回值是数字
	set(gcf,'Visible','off');% 关闭当前图形
    test02('Visible','on');% 显示次界面图形
    
end



% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

test02代码:

function varargout = test02(varargin)
% TEST02 MATLAB code for test02.fig
%      TEST02, by itself, creates a new TEST02 or raises the existing
%      singleton*.
%
%      H = TEST02 returns the handle to a new TEST02 or the handle to
%      the existing singleton*.
%
%      TEST02('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEST02.M with the given input arguments.
%
%      TEST02('Property','Value',...) creates a new TEST02 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before test02_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to test02_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 test02

% Last Modified by GUIDE v2.5 12-Sep-2021 15:31:14

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @test02_OpeningFcn, ...
                   'gui_OutputFcn',  @test02_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 test02 is made visible.
function test02_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 test02 (see VARARGIN)

% Choose default command line output for test02
handles.output = hObject;
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 两句代码搞定
global str
set(handles.edit1,'String',str);% 将获取到的鼠标单击的选项写入次界面的输入框中
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes test02 wait for user response (see UIRESUME)
% uiwait(handles.nextfig);


% --- Outputs from this function are returned to the command line.
function varargout = test02_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 pushbutton1.
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)
% 点击按钮时次界面隐藏,主界面显示
set(gcf,'Visible','off');
test('Visible','on');



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

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double

注意:global str ;set(handles.edit1,‘String’,str);% 将获取到的鼠标单击的选项写入次界面的输入框中
以上两句代码是写在OpeningFcn函数中,而OpeningFcn函数是在界面没出来之前就执行的,方便进行一些初始化的操作。结合test02(‘Visible’,‘on’)使用。

方法二:采用findall函数传递数据

将此代码添加到主界面的列表框回调函数中即可。
test代码:

if isequal(get(gcf,'SelectionType'),'normal')
     n = get(hObject,'value');%获取所选中列表的索引号
     str_all = get(hObject,'string');%得到列表框的所有文本
     set(gcf,'Visible','off');%隐藏主界面
     h = figure(test02);%打开次界面并获取去窗口句柄,若次界面已经打开,获取其句柄
     set(h,'Visible','on');% 设置次界面是可见的
     h_edit = findall(h,'Tag','edit1');% 在次界面中查好可编辑文本框的内容
     set(h_edit,'string',str_all{n}); %将鼠标单击内容显示在上一句所找到的文本框里面。
    

test02代码:

在按钮下面添加此代码

set(gcf,'Visible','off');%隐藏次界面
h = firgure(test);% 打开主界面并且获取其窗口句柄值
set(h,'Visible','on');% 设置主界面可见


方法二:采用findall函数传递数据

  • 9
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南霹雳堂雷家雷无桀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值