MATLAB笔记:GUI中的回调函数之间的数据传输

文章目录

效果

在这里插入图片描述
plot 是画图
hold是hold on
show是显示
clear是清除坐标轴

代码

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

% Last Modified by GUIDE v2.5 09-Oct-2023 20:03:32

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

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

% Update handles structure
guidata(hObject, handles);
func_init_para(handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = untitled_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 Para;
func_get_listbox(handles);

% --- 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

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)   %plot
% 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)
func_plot(handles);

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)   %hold
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Para;
Para.hold_tile = get(handles.pushbutton2,'String');
if strcmp(Para.hold_tile,'hold')
    set(handles.pushbutton2,'String','unhold');
else
    set(handles.pushbutton2,'String','hold');
end

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)   %clear
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Para;
cla(Para.axes);
legend(Para.axes,'off')
title(Para.axes,{})
xlabel(Para.axes,{})
ylabel(Para.axes,{})
Para.DisplayName = {};

% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)   %show
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Para;
func_get_listbox(handles);
tmpinfo = Para.lbox2_info;
tmpinfo = char(tmpinfo{:});
pha = Para.(tmpinfo);
fra = Para.x;
set(handles.uitable1,'Data', [fra',pha']);  % 鍦ㄨ〃鏍间腑鏄剧ず
set(handles.uitable1,'ColumnName' , {'x',tmpinfo});

function func_get_listbox(handles)
global Para;
str_1 = get(handles.listbox1,'String');
val_1 = get(handles.listbox1,'Value');
Para.lbox2_info = str_1(val_1);

function func_plot(handles)
global Para;
func_get_listbox(handles);
tmpinfo = Para.lbox2_info;
tmpinfo = char(tmpinfo{:});
pha = Para.(tmpinfo);
fra = Para.x;
Para.hold_tile = get(handles.pushbutton2,'String');
if strcmp(Para.hold_tile,'hold')
    hold(Para.axes,'on');
else
    hold(Para.axes,'off');
end
Para.fig = plot(Para.axes,fra,pha);
grid(Para.axes,'on');
xlabel(Para.axes,'x')
ylabel(Para.axes,tmpinfo)
Para.DisplayInfo = strcat(tmpinfo);
func_add_DisplayName;


function func_add_DisplayName
global Para;
DisplayInfo = Para.DisplayInfo;
if isempty(Para.DisplayName)
    Para.DisplayName{1} = DisplayInfo;
else
    Para.DisplayName{size(Para.DisplayName,2)+1} = DisplayInfo;
end
legend(Para.axes,Para.DisplayName);

function func_init_para(handles)
global Para;
Para.DisplayName = [];
Para.axes = handles.axes1;
handles.axes1.Toolbar.Visible = 'on';
Para.x = -pi+0.01:0.01:pi-0.01; 
Para.sin = sin(Para.x);
Para.cos = cos(Para.x);
Para.tan = tan(Para.x);
Para.cot = cot(Para.x);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MATLAB GUI 回调函数是指在 GUI 界面,用户进行某些操作(例如点击按钮、修改文本框内容等)时,系统会自动调用的一个函数。回调函数通常被用来响应用户的操作,执行一些特定的任务,例如更新界面、处理数据等。 在 MATLAB ,可以使用 GUIDE 工具箱来创建 GUI 界面,并为界面上的控件添加回调函数。具体步骤如下: 1. 打开 GUIDE 工具箱,在工具箱创建一个新的 GUI 界面。 2. 在界面上添加控件,例如按钮、文本框等。 3. 选一个控件,然后在右侧属性编辑器找到 Callbacks 选项,点击 Add Callback 按钮,即可创建一个新的回调函数。 4. 在新建回调函数的编辑器,编写回调函数的代码。回调函数的第一个输入参数通常是控件本身,另外可以添加其他输入参数,例如事件对象等。 5. 完成回调函数的编写后,保存 GUI 界面,然后在 MATLAB 运行该界面即可。 示例代码: ```matlab function myButton_Callback(hObject, eventdata, handles) % hObject handle to myButton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % 在这里编写按钮点击事件的代码 disp('Button clicked!') ``` 在这个例子,我们创建了一个名为 `myButton` 的按钮,并为该按钮添加了一个回调函数 `myButton_Callback()`。当用户点击按钮时,系统会自动调用回调函数,函数的代码会将一条消息打印到 MATLAB 命令行。 需要注意的是,回调函数的变量名 `handles` 是 MATLAB 自动生成的,用于存储 GUI 界面的控件句柄。我们可以使用这些句柄来操作界面控件,例如修改文本框内容、调整图像显示等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值