龙空技术网

MATLAB的guide制作平抛运动GUI界面步骤加程序

云龙派 7178

前言:

眼前朋友们对“matlab新画布”可能比较关怀,各位老铁们都想要剖析一些“matlab新画布”的相关内容。那么小编也在网摘上汇集了一些关于“matlab新画布””的相关文章,希望看官们能喜欢,兄弟们快快来了解一下吧!

 平抛运动:物体以一定的初速度沿水平方向抛出,如果物体运动过程中仅受重力作用。平抛运动可看作:水平方向的匀速直线运动以及竖直方向做自由落体运动的合运动。

1.界面设计

MATLAB提供了可视化的用户界面开发环境(GUDevelopment Environment, GUIDE),实现“所见即所得”。GUIDE可方便的创建GuI应用程序,它可以根据用户设计的GUI布局,自动生成M文件的框架,用户使用这一框架编制自己的应用程序。在MATLAB的命令行窗口中键入guide可以打开GUIDE。不过目前matlab更推荐使用appdesigner。

步骤1:在MATLAB命令窗口输入guide

Blank GUI(默认)一GUI with Uicontrols(带控件对象的GUI模板)GUI with Axes and Menu(带坐标轴与菜单的GUI模板)Modal Question Dialog(带模式问话对话框的GUI模板)

选择模板创建gui后,保存文件后,会产生.fig文件以及一个同名字的M文件(注:不可更改为不一致的文件名,否则运行要出错,如果需要修改就是要把程序中的所有相同的名称进行修改)。

步骤2:设计并完成GUI的图形控件布局

完成布局后fig界面如下

使用的控件包括静态文本控件、可编辑文本控件、面板控件、普通按钮控件、坐标区控件。

对于控件和界面画布要统一修改的属性是FontUnits(字体单位)、Units(测量单位属性)。因为不同人的电脑的屏幕大小不同,我们设计的GUI界面的控件大小最好是要能够适应所有尺寸的大小,因为将FontUnits和 Units属性都设置为normalized属性。

(1)FontUnits - 字体单位

'points' (默认) | 'normalized' | 'inches' | 'centimeters' | 'pixels'

字体单位,指定为下表中的值之一。

单位值

说明

'points'

磅。1 磅等于 1/72 英寸。

'normalized'

归一化值,将字体大小指定为高度的一定比例。当您调整 UI 组件的大小时,MATLAB 会缩放显示的字体以保持该比例。

'inches'

英寸。

'centimeters'

厘米。

'pixels'

像素。

(2) Units - 测量单位

'pixels' (默认) | 'normalized' | 'inches' | 'centimeters' | 'points' | 'characters'

步骤3 设置各个按键的回调函数以及相应控件的程序

(1)修改界面左上角的名称的程序要在GUI的打开的回调函数中执行。

function pingpaoGUI_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 pingpaoGUI (see VARARGIN)% Choose default command line output for pingpaoGUIhandles.output = hObject;movegui(gcf,'center');set(gcf,'NumberTitle','off','Name','平抛运动GUI计算系统');% Update handles structureguidata(hObject, handles);% UIWAIT makes pingpaoGUI wait for user response (see UIRESUME)% uiwait(handles.figure1);

(2)开始计算按键的回调函数,将平抛运动的过程转化为编程语言,同时获取平抛运动的参数,实现过程绘图。

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)H = str2num(get(handles.edit1,'string'));v0= str2num(get(handles.edit2,'string'));g = 9.8;T= sqrt(2*H/g);X= v0*T;t = 0:0.01:T;xt = v0.*t;yt = v0*T-1/2*g.*t;S = H-1/2.*g*(xt/v0).^2;vx = v0+zeros(1,length(t));vy = g.*t;v = sqrt(vx.^2+vy.^2);axes(handles.axes1);plot(xt,S,'linewidth',2);xlabel('水平位移(m)');ylabel('高度(m)');grid on;hold on;title('小球运行轨迹图');axes(handles.axes2);plot(t,v,'linewidth',2);xlabel('时间(s)');ylabel('速度(m/s)');grid on;hold on;title('小球速度随时间变化图');set(handles.edit3,'string',num2str(T));set(handles.edit4,'string',num2str(X));

(3)清除按键,将输入的数据中的文本设置为空,同时将在坐标区控件上的内容清除。

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)set(handles.edit1,'string','');set(handles.edit2,'string','');set(handles.edit3,'string','');set(handles.edit4,'string','');try    delete(allchild(handles.axes1));endtry    delete(allchild(handles.axes2));end

(4)关闭按键,关闭当前的GUI界面。

function pushbutton3_Callback(hObject, eventdata, handles)% 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)closereq;

2.界面使用说明

基于MATLAB的GUI界面设计了一个操作简单平抛运动GUI计算界面如下:

点击运行pingpaoGUI.m——输入平抛运动的抛出高度和水平初速度——点击开始计算即可出现结果。需要GUI的.m程序和fig文件,可以进行打赏后截图(10元及以上),进行联系,或者在微信公众号云龙派,点击“联系掌门”进行联系,或者在公众号内回复截图,几小时内会回复。界面编程不易,还请见谅!注:平抛运动GUI的完整程序在文章结尾部分,可以自行观看操作视频通过guide设计gui界面,实现相应的功能。

2.界面举例计算

Step1:输入平抛运动的高度和初速度

Step2:点击开始运动

Step3:可以继续输入新的高度,初速度保持不变。

Step3:可以继续输入新的初速度,高度保持不变。

3.GUI完整程序如下

function varargout = pingpaoGUI(varargin)% PINGPAOGUI MATLAB code for pingpaoGUI.fig%      PINGPAOGUI, by itself, creates a new PINGPAOGUI or raises the existing%      singleton*.%%      H = PINGPAOGUI returns the handle to a new PINGPAOGUI or the handle to%      the existing singleton*.%%      PINGPAOGUI('CALLBACK',hObject,eventData,handles,...) calls the local%      function named CALLBACK in PINGPAOGUI.M with the given input arguments.%%      PINGPAOGUI('Property','Value',...) creates a new PINGPAOGUI or raises the%      existing singleton*.  Starting from the left, property value pairs are%      applied to the GUI before pingpaoGUI_OpeningFcn gets called.  An%      unrecognized property name or invalid value makes property application%      stop.  All inputs are passed to pingpaoGUI_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 pingpaoGUI% Last Modified by GUIDE v2.5 15-Dec-2022 19:57:46% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name',       mfilename, ...                   'gui_Singleton',  gui_Singleton, ...                   'gui_OpeningFcn', @pingpaoGUI_OpeningFcn, ...                   'gui_OutputFcn',  @pingpaoGUI_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 pingpaoGUI is made visible.function pingpaoGUI_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 pingpaoGUI (see VARARGIN)% Choose default command line output for pingpaoGUIhandles.output = hObject;movegui(gcf,'center');set(gcf,'NumberTitle','off','Name','平抛运动GUI计算系统');% Update handles structureguidata(hObject, handles);% UIWAIT makes pingpaoGUI wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = pingpaoGUI_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;% --- 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)H = str2num(get(handles.edit1,'string'));v0= str2num(get(handles.edit2,'string'));g = 9.8;T= sqrt(2*H/g);X= v0*T;t = 0:0.01:T;xt = v0.*t;yt = v0*T-1/2*g.*t;S = H-1/2.*g*(xt/v0).^2;vx = v0+zeros(1,length(t));vy = g.*t;v = sqrt(vx.^2+vy.^2);axes(handles.axes1);plot(xt,S,'linewidth',2);xlabel('水平位移(m)');ylabel('高度(m)');grid on;hold on;title('小球运行轨迹图');axes(handles.axes2);plot(t,v,'linewidth',2);xlabel('时间(s)');ylabel('速度(m/s)');grid on;hold on;title('小球速度随时间变化图');set(handles.edit3,'string',num2str(T));set(handles.edit4,'string',num2str(X));% --- 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)set(handles.edit1,'string','');set(handles.edit2,'string','');set(handles.edit3,'string','');set(handles.edit4,'string','');try    delete(allchild(handles.axes1));endtry    delete(allchild(handles.axes2));end% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% 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)closereq;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');end

本文内容来源于网络,仅供参考学习,如内容、图片有任何版权问题,请联系处理,24小时内删除。

作 者 | 郭志龙

编 辑 | 郭志龙

校 对 | 郭志龙

标签: #matlab新画布 #matlab怎样调整画布的大小 #matlab画布 #matlab的布局在哪 #matlab画布功能