项目名称: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 zf=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 EDITgui_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});endif 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 untitled1handles.output = hObject;% Update handles structureguidata(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 structurevarargout{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');endfunction 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');endfunction 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');endfunction 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');endfunction 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');endfunction 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');endfunction 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 zf=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');endfunction 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