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

### 回答1: 在 Matlab GUI 中设置坐标轴可以使用函数 "axes"。例如,可以使用以下代码在 GUI 中创建一个坐标轴并将其命名为 "myaxes": ``` myaxes = axes('Parent', myfigure); ``` 在这里,"myfigure" 是已经创建的 GUI 窗口。 您还可以使用 "xlabel","ylabel" 和 "zlabel" 函数为坐标轴添加标签,例如: ``` xlabel(myaxes,'X-axis'); ylabel(myaxes,'Y-axis'); zlabel(myaxes,'Z-axis'); ``` 您还可以使用"set"函数来设置坐标轴的属性,例如: ``` set(myaxes,'XGrid','on','YGrid','on','ZGrid','on'); ``` 上面的代码将开启X,Y,Z轴的网格线 最后请注意,在绘制形之前,需要先选定当前坐标轴。可以使用以下代码选定 "myaxes" 坐标轴: ``` axes(myaxes); ``` 希望这些信息能帮助你! ### 回答2: MATLAB是一种广泛应用于数学、工程等领域的计算软件,而GUIMATLAB提供的一种形用户界面设计工具。在MATLAB中,存在着用于绘制像的函数axes,它可以被嵌入到GUI界面中用于显示二维像和三维像。axes提供了设置像中坐标轴的功能,下面将详细介绍如何设置MATLAB GUI axes坐标轴。 首先,我们需要先创建一个绘对象,比如像,使用MATLAB的imshow或plot函数,将绘对象置于axes控件中。接着,在代码中使用axes对象的属性来更改坐标轴的绘制样式,包括轴范围、轴标签、轴线以及其他用于地绘制和数据可视化等功能的属性设置。具体来说,我们要注意以下几点: 1.设置轴范围:可以使用axes的xlim和ylim属性来设置每个轴的坐标范围,比如:axes.XLim = [x_min x_max]; 2.设置轴标签:可以使用xlabel、ylabel和title函数分别设置x轴、y轴和整个像的标题,比如:xlabel('x label'); 3.设置轴刻度:可以使用xticks、yticks和xticklabels、yticklabels等函数设置坐标轴刻度线以及标记,比如:xticks(x_tick_location); 4.设置轴线:可以使用axes的XColor、YColor、ZColor属性来设置不同轴线的颜色和样式,通过LineWidth属性设置轴线宽度,比如:axes.XColor = [1 0 0]; axes.XGrid = 'on'; 5.设置网格线:可以使用xgrid、ygrid函数显示网格线,并使用XGrid、YGrid或ZGrid属性来控制是否显示网格线,比如:xgrid on; 总的来说,以上这些属性的设置是非常灵活的,它们使我们可以控制axes的显示方式和样式,使其更符合我们的需求。当然,axes的其他属性设置还包括了坐标轴的长度、位置、字体大小、颜色等,这些属性可以结合不同的情景进行合理设置,从而使我们的像具有更好的可视化效果。 综上所述,设置MATLAB GUI axes坐标轴需要熟练掌握axes的各种属性,并结合项目需求进行合理的设置。在实际使用中,我们需要对axes更深入的了解和掌握,以便更好的利用它进行数据可视化和绘制。 ### 回答3: Matlab是一个强大的集成开发环境,它支持许多不同的编程应用场景,包括开发GUI形用户界面)。在MatlabGUI中,关键部分之一是像,其绘制和操作大多依赖于Axes对象(坐标轴对象)。本文将介绍如何使用Matlab GUI Axes设置坐标轴。 首先,打开Matlab GUI,在GUIDE(GUI开发环境)中选择Axes组件,然后将其拖放到你的GUI窗口中。默认情况下,Axes组件将会显示一个空白的平面坐标,它允许用户绘制直线、散点和其他形。Axes组件还提供了许多其他功能,包括设置坐标轴范围、刻度、标签等。 其次,设置坐标轴的范围。在MATLAB GUI Axes中,你可以使用“xlim”和“ylim”等命令设置坐标轴的范围。例如,如果你想设置x轴范围为0到10,而y轴范围为-5到5,可以使用以下命令: xlim([0 10]) ylim([-5 5]) 这将使你的Axes组件显示在坐标轴范围内的任何像。 第三,设置坐标轴的刻度(tickmarks)。Axes组件提供了一系列用于设置坐标轴刻度的命令,例如: set(gca,'XTick',[0:1:10]) set(gca,'YTick',[-5:1:5]) 该命令将设置x轴的刻度为0到10以1的间隔,y轴的刻度为-5到5以1的间隔。刻度还可以通过“XTicklabel”和“YTicklabel”命令设置标签。 第四,设置坐标轴的标签。你可以使用“xlabel”和“ylabel”命令设置x轴和y轴的标签: xlabel('x轴') ylabel('y轴') 最后,还可以设置其他参数,例如坐标轴的标题、字体大小、颜色等等。总的来说,MATLAB GUI的Axes组件提供了多种方法来设置坐标轴,这些方法可以通过命令行或GUI工具栏完成。熟练操作这些命令可以使你更好地进行Matlab GUI设计和开发。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值