Matlab课程设计(GUI)——绘制任意函数图

项目名称:matlabGUI设计——函数图像绘制

项目简介

MATLAB GUI 编程是一个功能强大的工具,可以帮助我们构建交互式的用户界面并处理用户输入。通过获取编辑框对象的文本内容、设置图形属性和绘制函数图形,实现了按下按钮事件绘制图像的功能。

功能特点

-可视化绘制

-有平面和立体图像

-可自定范围

-自由度极高

-可二次开发

使用方法

1.打开matlab gui

在matlab命令行窗口输入"guide",敲下回车,选择打开现有gui,选择fig文件

点击打开,出现gui编辑框

然后再点击运行,就可以看到输入框、生成图案的按键啦

 

2.获取用户输入

回调函数可以通过获取编辑框对象的文本内容,获得用户在界面中输入的数据。这使得我们能够在按钮按下事件中获取用户输入的值,用于进一步的处理和计算。

此时我们在输入解析式这边输入我们想要绘制的函数:我们以x^2+y^2=4为例,绘制图像

注:我是直接默认输入的解析式=0了,所以我们输入x^2+y^2-4

3.修改图形属性

回调函数可以使用 set 函数来修改图形对象的各种属性,如线条颜色、线宽、字体样式等。通过修改这些属性,我们可以实现对图形的定制化显示。

按照这个输入:颜色选红色就输入r。(若是蓝色就输入b,黄色就是y,英文首字母..)然后线宽可以选任意数字,我们以2为例,范围就看你想要多大了。

4.绘制图形

回调函数可以利用 MATLAB 提供的绘图函数(如 ezplot 和 ezmesh)绘制二维或三维的函数图形。这些函数可以根据输入的公式和坐标范围,自动生成具有可视化效果的函数图形,方便用户进行数据分析和可视化。

当进行完上面三个步骤,到这里就只需要点点两个按钮(2D、3D)就可以看到图像啦

最大值点和最小值点功能还没做出来,要是有大佬可以指点就更好啦0.o

程序讲解

二维图像的绘制

pushbutton1_Callback:

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)
fd = get(handles.edit1,'String');  %公式输入框
axes(handles.axes1);
Xmin = get(handles.edit4,'String');
Xmax = get(handles.edit5,'String');
Ymin = get(handles.edit6,'String');
Ymax = get(handles.edit7,'String');

f1 = ezplot(fd,[str2num(Xmin),str2num(Xmax),str2num(Ymin),str2num(Ymax)]);
col = get(handles.edit2,'String');
set(f1,'Color',col);
wid = get(handles.edit3,'String');
set(f1,'LineWidth',str2num(wid));
grid on;

三维图像的绘制:

pushbutton2_Callback:

function pushbutton2_Callback(hObject, eventdata, handles)
% 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)
fd = get(handles.edit1,'String');
axes(handles.axes2);
syms x y z
f=get(handles.edit1,'String');
Xmin = get(handles.edit4,'String');
Xmax = get(handles.edit5,'String');
Ymin = get(handles.edit6,'String');
Ymax = get(handles.edit7,'String');
ezmesh(f,[str2num(Xmin),str2num(Xmax),str2num(Ymin),str2num(Ymax)])
grid on;

完成程序:

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

% Last Modified by GUIDE v2.5 17-Feb-2023 18:05:36

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = untitled1_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;



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


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

% Hint: edit 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



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


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

% Hint: edit 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



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


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

% Hint: edit 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



function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


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

% Hint: edit 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



function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (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 edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


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

% Hint: edit 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



function edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (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 edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


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

% Hint: edit 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



function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (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 edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


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

% Hint: edit 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)
% 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)
fd = get(handles.edit1,'String');  %公式输入框
axes(handles.axes1);
Xmin = get(handles.edit4,'String');
Xmax = get(handles.edit5,'String');
Ymin = get(handles.edit6,'String');
Ymax = get(handles.edit7,'String');

f1 = ezplot(fd,[str2num(Xmin),str2num(Xmax),str2num(Ymin),str2num(Ymax)]);
col = get(handles.edit2,'String');
set(f1,'Color',col);
wid = get(handles.edit3,'String');
set(f1,'LineWidth',str2num(wid));
grid on;


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% 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)
fd = get(handles.edit1,'String');
axes(handles.axes2);
syms x y z
f=get(handles.edit1,'String');
Xmin = get(handles.edit4,'String');
Xmax = get(handles.edit5,'String');
Ymin = get(handles.edit6,'String');
Ymax = get(handles.edit7,'String');
ezmesh(f,[str2num(Xmin),str2num(Xmax),str2num(Ymin),str2num(Ymax)])
grid on;



function edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (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 edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double


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

% Hint: edit 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



function edit9_Callback(hObject, eventdata, handles)
% hObject    handle to edit9 (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 edit9 as text
%        str2double(get(hObject,'String')) returns contents of edit9 as a double


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

% Hint: edit 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

资源下载

百度网盘:链接:https://pan.baidu.com/s/1WSsJEBMhrM27-qkd1stUEA?pwd=gjbn 
提取码:gjbn

github链接:GitHub - sy0045/matlab-gui: great gui

### 如何在MATLAB中创建包含坐标轴的形用户界面(GUI) #### 使用GUIDE创建GUI 为了创建一个带有坐标轴的GUI,可以利用MATLAB中的GUIDE工具。GUIDE提供了可视化的设计环境来布置各种UI组件,包括按钮、滑块以及坐标轴等[^1]。 启动GUIDE可以通过命令行输入`guide`并按回车键实现。首次运行时会弹出一个对话框让用户选择是从空白模板开始还是基于现有文件打开项目。对于新手来说推荐先尝试简单的布局再逐步增加复杂度。 #### 添加Axes到GUI 当选择了合适的模板之后进入编辑器窗口,在这里能够看到左侧有一个控件面板列出了所有可用的小部件选项。“Axes”就是用来显示像数据的关键元素之一;将其拖放到设计区内的任意位置即可完成添加操作[^2]。 放置好axes后还可以调整其大小形状以适应整体界面需求,并通过属性检查器设置更多细节参数比如标签文字颜色等等。 #### 编写回调函数绘制表 为了让这个新加入的坐标区域真正发挥作用,则需编写相应的m文件代码逻辑——即定义事件触发后的响应行为(如点击某个按键)。假设现在要实现在按下特定Button后于刚才设定好的Axes里画一条正弦曲线: ```matlab function pushbutton_Callback(hObject, eventdata, handles) % hObject 当前pushbutton句柄 % eventdata 额外的数据传递给回调函数 (一般为空) % handles 结构体; 脚本初始化期间保存的所有GUI状态信息 x = linspace(0, 2*pi); y = sin(x); axes(handles.axes1); % 切换当前绘目标至指定的axes plot(x,y,'r'); % 绘制红色线条表示sin波形 title('Sine Wave'); xlabel('X Axis'); ylabel('Y Axis'); grid on; ``` 上述例子展示了如何关联按钮动作与实际作过程之间的联系。每当用户交互发生改变都会调用对应的处理程序执行预设指令序列最终达到动态更新视窗内展示效果的目的[^3]。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值