当前位置:   article > 正文

Matlab课程设计(GUI)——绘制任意函数图_用matlab的gui做函数图像

用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:

  1. function pushbutton1_Callback(hObject, eventdata, handles)
  2. % hObject handle to pushbutton1 (see GCBO)
  3. % eventdata reserved - to be defined in a future version of MATLAB
  4. % handles structure with handles and user data (see GUIDATA)
  5. fd = get(handles.edit1,'String'); %公式输入框
  6. axes(handles.axes1);
  7. Xmin = get(handles.edit4,'String');
  8. Xmax = get(handles.edit5,'String');
  9. Ymin = get(handles.edit6,'String');
  10. Ymax = get(handles.edit7,'String');
  11. f1 = ezplot(fd,[str2num(Xmin),str2num(Xmax),str2num(Ymin),str2num(Ymax)]);
  12. col = get(handles.edit2,'String');
  13. set(f1,'Color',col);
  14. wid = get(handles.edit3,'String');
  15. set(f1,'LineWidth',str2num(wid));
  16. grid on;

三维图像的绘制:

pushbutton2_Callback:

  1. function pushbutton2_Callback(hObject, eventdata, handles)
  2. % hObject handle to pushbutton2 (see GCBO)
  3. % eventdata reserved - to be defined in a future version of MATLAB
  4. % handles structure with handles and user data (see GUIDATA)
  5. fd = get(handles.edit1,'String');
  6. axes(handles.axes2);
  7. syms x y z
  8. f=get(handles.edit1,'String');
  9. Xmin = get(handles.edit4,'String');
  10. Xmax = get(handles.edit5,'String');
  11. Ymin = get(handles.edit6,'String');
  12. Ymax = get(handles.edit7,'String');
  13. ezmesh(f,[str2num(Xmin),str2num(Xmax),str2num(Ymin),str2num(Ymax)])
  14. grid on;

完成程序:

  1. function varargout = untitled1(varargin)
  2. % UNTITLED1 MATLAB code for untitled1.fig
  3. % UNTITLED1, by itself, creates a new UNTITLED1 or raises the existing
  4. % singleton*.
  5. %
  6. % H = UNTITLED1 returns the handle to a new UNTITLED1 or the handle to
  7. % the existing singleton*.
  8. %
  9. % UNTITLED1('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in UNTITLED1.M with the given input arguments.
  11. %
  12. % UNTITLED1('Property','Value',...) creates a new UNTITLED1 or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before untitled1_OpeningFcn gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to untitled1_OpeningFcn via varargin.
  17. %
  18. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
  19. % instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES
  22. % Edit the above text to modify the response to help untitled1
  23. % Last Modified by GUIDE v2.5 17-Feb-2023 18:05:36
  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct('gui_Name', mfilename, ...
  27. 'gui_Singleton', gui_Singleton, ...
  28. 'gui_OpeningFcn', @untitled1_OpeningFcn, ...
  29. 'gui_OutputFcn', @untitled1_OutputFcn, ...
  30. 'gui_LayoutFcn', [] , ...
  31. 'gui_Callback', []);
  32. if nargin && ischar(varargin{1})
  33. gui_State.gui_Callback = str2func(varargin{1});
  34. end
  35. if nargout
  36. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38. gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT
  41. % --- Executes just before untitled1 is made visible.
  42. function untitled1_OpeningFcn(hObject, eventdata, handles, varargin)
  43. % This function has no output args, see OutputFcn.
  44. % hObject handle to figure
  45. % eventdata reserved - to be defined in a future version of MATLAB
  46. % handles structure with handles and user data (see GUIDATA)
  47. % varargin command line arguments to untitled1 (see VARARGIN)
  48. % Choose default command line output for untitled1
  49. handles.output = hObject;
  50. % Update handles structure
  51. guidata(hObject, handles);
  52. % UIWAIT makes untitled1 wait for user response (see UIRESUME)
  53. % uiwait(handles.figure1);
  54. % --- Outputs from this function are returned to the command line.
  55. function varargout = untitled1_OutputFcn(hObject, eventdata, handles)
  56. % varargout cell array for returning output args (see VARARGOUT);
  57. % hObject handle to figure
  58. % eventdata reserved - to be defined in a future version of MATLAB
  59. % handles structure with handles and user data (see GUIDATA)
  60. % Get default command line output from handles structure
  61. varargout{1} = handles.output;
  62. function edit1_Callback(hObject, eventdata, handles)
  63. % hObject handle to edit1 (see GCBO)
  64. % eventdata reserved - to be defined in a future version of MATLAB
  65. % handles structure with handles and user data (see GUIDATA)
  66. % Hints: get(hObject,'String') returns contents of edit1 as text
  67. % str2double(get(hObject,'String')) returns contents of edit1 as a double
  68. % --- Executes during object creation, after setting all properties.
  69. function edit1_CreateFcn(hObject, eventdata, handles)
  70. % hObject handle to edit1 (see GCBO)
  71. % eventdata reserved - to be defined in a future version of MATLAB
  72. % handles empty - handles not created until after all CreateFcns called
  73. % Hint: edit controls usually have a white background on Windows.
  74. % See ISPC and COMPUTER.
  75. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  76. set(hObject,'BackgroundColor','white');
  77. end
  78. function edit2_Callback(hObject, eventdata, handles)
  79. % hObject handle to edit2 (see GCBO)
  80. % eventdata reserved - to be defined in a future version of MATLAB
  81. % handles structure with handles and user data (see GUIDATA)
  82. % Hints: get(hObject,'String') returns contents of edit2 as text
  83. % str2double(get(hObject,'String')) returns contents of edit2 as a double
  84. % --- Executes during object creation, after setting all properties.
  85. function edit2_CreateFcn(hObject, eventdata, handles)
  86. % hObject handle to edit2 (see GCBO)
  87. % eventdata reserved - to be defined in a future version of MATLAB
  88. % handles empty - handles not created until after all CreateFcns called
  89. % Hint: edit controls usually have a white background on Windows.
  90. % See ISPC and COMPUTER.
  91. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  92. set(hObject,'BackgroundColor','white');
  93. end
  94. function edit3_Callback(hObject, eventdata, handles)
  95. % hObject handle to edit3 (see GCBO)
  96. % eventdata reserved - to be defined in a future version of MATLAB
  97. % handles structure with handles and user data (see GUIDATA)
  98. % Hints: get(hObject,'String') returns contents of edit3 as text
  99. % str2double(get(hObject,'String')) returns contents of edit3 as a double
  100. % --- Executes during object creation, after setting all properties.
  101. function edit3_CreateFcn(hObject, eventdata, handles)
  102. % hObject handle to edit3 (see GCBO)
  103. % eventdata reserved - to be defined in a future version of MATLAB
  104. % handles empty - handles not created until after all CreateFcns called
  105. % Hint: edit controls usually have a white background on Windows.
  106. % See ISPC and COMPUTER.
  107. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  108. set(hObject,'BackgroundColor','white');
  109. end
  110. function edit4_Callback(hObject, eventdata, handles)
  111. % hObject handle to edit4 (see GCBO)
  112. % eventdata reserved - to be defined in a future version of MATLAB
  113. % handles structure with handles and user data (see GUIDATA)
  114. % Hints: get(hObject,'String') returns contents of edit4 as text
  115. % str2double(get(hObject,'String')) returns contents of edit4 as a double
  116. % --- Executes during object creation, after setting all properties.
  117. function edit4_CreateFcn(hObject, eventdata, handles)
  118. % hObject handle to edit4 (see GCBO)
  119. % eventdata reserved - to be defined in a future version of MATLAB
  120. % handles empty - handles not created until after all CreateFcns called
  121. % Hint: edit controls usually have a white background on Windows.
  122. % See ISPC and COMPUTER.
  123. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  124. set(hObject,'BackgroundColor','white');
  125. end
  126. function edit5_Callback(hObject, eventdata, handles)
  127. % hObject handle to edit5 (see GCBO)
  128. % eventdata reserved - to be defined in a future version of MATLAB
  129. % handles structure with handles and user data (see GUIDATA)
  130. % Hints: get(hObject,'String') returns contents of edit5 as text
  131. % str2double(get(hObject,'String')) returns contents of edit5 as a double
  132. % --- Executes during object creation, after setting all properties.
  133. function edit5_CreateFcn(hObject, eventdata, handles)
  134. % hObject handle to edit5 (see GCBO)
  135. % eventdata reserved - to be defined in a future version of MATLAB
  136. % handles empty - handles not created until after all CreateFcns called
  137. % Hint: edit controls usually have a white background on Windows.
  138. % See ISPC and COMPUTER.
  139. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  140. set(hObject,'BackgroundColor','white');
  141. end
  142. function edit6_Callback(hObject, eventdata, handles)
  143. % hObject handle to edit6 (see GCBO)
  144. % eventdata reserved - to be defined in a future version of MATLAB
  145. % handles structure with handles and user data (see GUIDATA)
  146. % Hints: get(hObject,'String') returns contents of edit6 as text
  147. % str2double(get(hObject,'String')) returns contents of edit6 as a double
  148. % --- Executes during object creation, after setting all properties.
  149. function edit6_CreateFcn(hObject, eventdata, handles)
  150. % hObject handle to edit6 (see GCBO)
  151. % eventdata reserved - to be defined in a future version of MATLAB
  152. % handles empty - handles not created until after all CreateFcns called
  153. % Hint: edit controls usually have a white background on Windows.
  154. % See ISPC and COMPUTER.
  155. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  156. set(hObject,'BackgroundColor','white');
  157. end
  158. function edit7_Callback(hObject, eventdata, handles)
  159. % hObject handle to edit7 (see GCBO)
  160. % eventdata reserved - to be defined in a future version of MATLAB
  161. % handles structure with handles and user data (see GUIDATA)
  162. % Hints: get(hObject,'String') returns contents of edit7 as text
  163. % str2double(get(hObject,'String')) returns contents of edit7 as a double
  164. % --- Executes during object creation, after setting all properties.
  165. function edit7_CreateFcn(hObject, eventdata, handles)
  166. % hObject handle to edit7 (see GCBO)
  167. % eventdata reserved - to be defined in a future version of MATLAB
  168. % handles empty - handles not created until after all CreateFcns called
  169. % Hint: edit controls usually have a white background on Windows.
  170. % See ISPC and COMPUTER.
  171. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  172. set(hObject,'BackgroundColor','white');
  173. end
  174. % --- Executes on button press in pushbutton1.
  175. function pushbutton1_Callback(hObject, eventdata, handles)
  176. % hObject handle to pushbutton1 (see GCBO)
  177. % eventdata reserved - to be defined in a future version of MATLAB
  178. % handles structure with handles and user data (see GUIDATA)
  179. fd = get(handles.edit1,'String'); %公式输入框
  180. axes(handles.axes1);
  181. Xmin = get(handles.edit4,'String');
  182. Xmax = get(handles.edit5,'String');
  183. Ymin = get(handles.edit6,'String');
  184. Ymax = get(handles.edit7,'String');
  185. f1 = ezplot(fd,[str2num(Xmin),str2num(Xmax),str2num(Ymin),str2num(Ymax)]);
  186. col = get(handles.edit2,'String');
  187. set(f1,'Color',col);
  188. wid = get(handles.edit3,'String');
  189. set(f1,'LineWidth',str2num(wid));
  190. grid on;
  191. % --- Executes on button press in pushbutton2.
  192. function pushbutton2_Callback(hObject, eventdata, handles)
  193. % hObject handle to pushbutton2 (see GCBO)
  194. % eventdata reserved - to be defined in a future version of MATLAB
  195. % handles structure with handles and user data (see GUIDATA)
  196. fd = get(handles.edit1,'String');
  197. axes(handles.axes2);
  198. syms x y z
  199. f=get(handles.edit1,'String');
  200. Xmin = get(handles.edit4,'String');
  201. Xmax = get(handles.edit5,'String');
  202. Ymin = get(handles.edit6,'String');
  203. Ymax = get(handles.edit7,'String');
  204. ezmesh(f,[str2num(Xmin),str2num(Xmax),str2num(Ymin),str2num(Ymax)])
  205. grid on;
  206. function edit8_Callback(hObject, eventdata, handles)
  207. % hObject handle to edit8 (see GCBO)
  208. % eventdata reserved - to be defined in a future version of MATLAB
  209. % handles structure with handles and user data (see GUIDATA)
  210. % Hints: get(hObject,'String') returns contents of edit8 as text
  211. % str2double(get(hObject,'String')) returns contents of edit8 as a double
  212. % --- Executes during object creation, after setting all properties.
  213. function edit8_CreateFcn(hObject, eventdata, handles)
  214. % hObject handle to edit8 (see GCBO)
  215. % eventdata reserved - to be defined in a future version of MATLAB
  216. % handles empty - handles not created until after all CreateFcns called
  217. % Hint: edit controls usually have a white background on Windows.
  218. % See ISPC and COMPUTER.
  219. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  220. set(hObject,'BackgroundColor','white');
  221. end
  222. function edit9_Callback(hObject, eventdata, handles)
  223. % hObject handle to edit9 (see GCBO)
  224. % eventdata reserved - to be defined in a future version of MATLAB
  225. % handles structure with handles and user data (see GUIDATA)
  226. % Hints: get(hObject,'String') returns contents of edit9 as text
  227. % str2double(get(hObject,'String')) returns contents of edit9 as a double
  228. % --- Executes during object creation, after setting all properties.
  229. function edit9_CreateFcn(hObject, eventdata, handles)
  230. % hObject handle to edit9 (see GCBO)
  231. % eventdata reserved - to be defined in a future version of MATLAB
  232. % handles empty - handles not created until after all CreateFcns called
  233. % Hint: edit controls usually have a white background on Windows.
  234. % See ISPC and COMPUTER.
  235. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  236. set(hObject,'BackgroundColor','white');
  237. end

资源下载

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/327269
推荐阅读
相关标签
  

闽ICP备14008679号