(机电控制技术)MATLAB的appdesigner设计一个界面显示仿真数据

1. 效果展示

点击开始,显示数据:

2. 设计 

2.1 框架设计

首先,搭建框架,输入appdesigner打开appdesigner界面,新建保存一个新的app,从左侧组件库拉出构件,如坐标区、编辑字段(姓名学号)、标签(大标题)和按钮(开始),然后自己进行相关排列布局:

然后,点击每个构件,对每个构建的文字内容进行修改,例如坐标区的标题修改:

2.2 代码编写

2.2.1 代码视图

接着,去到代码视图,开始编写代码:

 

 1. 在代码的前面部分,进行了参数定义:

可以看到,我们有一个坐标区(UIAxes),一个按钮(Button)和一个底层图像(UIFigure),我们想要做的是:点击按钮,图像显示,这是很简单的一个过程。 

2.2.2 添加回调函数

 首先,点击左上角“编辑器”中的“回调”:

出现如下界面:

 在组件中选择“Button”,再点击“添加回调”,得到一个回调函数:

 我们就是要在这个回调函数中写我们想要执行的命令。

 2.2.3. 编写代码

1. 获取数据“simout”

            %%PID数据提取
            PID_ori = evalin('base', 'simout1');
            PID_control = evalin('base', 'simout2');

其中,“simout1”和“simout2”是我们从Simulink仿真中得到的仿真数据,在Simulink中双击左键,输入“To Workspace”,则可以得到“simout”模块,如下图所示:

 将得到的数据与“simout”连接则可以输出,这里我们以一个“step”模块和“sine wave”模块的输入信号分别作为“simout”值:

 点击仿真按钮,去MATLAB的工作区查看,可以看到得到了两个参数:

 2. 绘图

            %%数据展示
            plot(app.UIAxes,PID_ori.data , 'LineWidth',1, "Color",[1, 0 ,0]);
            hold(app.UIAxes, "on");
            plot(app.UIAxes, PID_control.data, "LineWidth",1,"Color",[0,1,0]);
            legend(app.UIAxes, ["PID ori", "PID control"]);

可以看到,还是使用plot函数,但是需要选择在哪一个“坐标区”上绘图 ,可以看到,这里我们是在“app.UIAxes”上绘图,这在我们“代码视图”的右边组件浏览器中可以看到名称:

 当“坐标区”数量多于1时,系统会自动进行编码:app.UIAxes_2、app.UIAxes_3等。

hold on用法与.m文件一致;legend是图例。

3. 运行

点击运行,结果如下所示:

 可以看到,运行成功,由于我并没有对位置进行规划之类的,所以UI有点惨不忍睹,如果想要修改,可以点击代码界面的右上角“设计视图”回到设计界面进行修改。

 

 4. 总结

进行了一次简单的MATLAB的appdesigner用法讲解,设计了一个粗糙的UI界面,主要适用于一些小型课程设计的UI界面设计要求(这里是用于机电控制技术的Homework project),完全可以实现UI设计速成,有想法和改进欢迎进行联系交流,如果本篇文章对你有帮助,解决了你的疑惑或者问题,请不要吝啬你的点赞哦,谢谢!

  • 10
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个简单的MATLAB App Designer登录界面设计: 1. 创建一个新的App Designer应用程序,命名为“LoginUI”; 2. 在设计视图中,选择“布局”选项卡,然后将“Grid布局”拖到窗体上; 3. 在布局网格中,可以添加标题、用户名、密码和登录按钮等元素,可以使用uieditfield和uilabel等组件来实现; 4. 可以在设计视图中使用“属性”窗口对每个元素进行自定义设置,例如设置标签文本、大小、颜色等; 5. 最后,可以使用回调函数编写相关的MATLAB代码来实现登录功能,例如检查用户名和密码是否正确,如果正确则进入主界面,否则显示错误消息。 以下是一个简单的MATLAB App Designer登录界面设计示例代码: ``` classdef LoginUI < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure GridLayout matlab.ui.container.GridLayout TitleLabel matlab.ui.control.Label UserLabel matlab.ui.control.Label UserEditField matlab.ui.control.EditField PassLabel matlab.ui.control.Label PassEditField matlab.ui.control.EditField LoginButton matlab.ui.control.Button end % Callbacks that handle component events methods (Access = private) % Button pushed function: LoginButton function LoginButtonPushed(app, event) % Check if username and password are correct username = app.UserEditField.Value; password = app.PassEditField.Value; if strcmp(username, 'admin') && strcmp(password, '12345') % Successful login, open main UI MainUI(app); % Close login UI delete(app); else % Incorrect username or password, show error message uialert(app.UIFigure, 'Invalid username or password.', 'Login Error'); end end end % App initialization and construction methods (Access = private) % Create UIFigure and components function createComponents(app) % Create UIFigure app.UIFigure = uifigure; app.UIFigure.Position = [100 100 300 200]; app.UIFigure.Name = 'LoginUI'; app.UIFigure.Resize = 'off'; % Create GridLayout app.GridLayout = uigridlayout(app.UIFigure); app.GridLayout.ColumnWidth = {'1x', '2x'}; app.GridLayout.RowHeight = {22, 22, 22, 22, '1x'}; % Create TitleLabel app.TitleLabel = uilabel(app.GridLayout); app.TitleLabel.HorizontalAlignment = 'center'; app.TitleLabel.FontSize = 16; app.TitleLabel.FontWeight = 'bold'; app.TitleLabel.Layout.Row = 1; app.TitleLabel.Layout.Column = [1 2]; app.TitleLabel.Text = 'Login'; % Create UserLabel app.UserLabel = uilabel(app.GridLayout); app.UserLabel.HorizontalAlignment = 'right'; app.UserLabel.Layout.Row = 2; app.UserLabel.Layout.Column = 1; app.UserLabel.Text = 'Username:'; % Create UserEditField app.UserEditField = uieditfield(app.GridLayout, 'text'); app.UserEditField.Layout.Row = 2; app.UserEditField.Layout.Column = 2; % Create PassLabel app.PassLabel = uilabel(app.GridLayout); app.PassLabel.HorizontalAlignment = 'right'; app.PassLabel.Layout.Row = 3; app.PassLabel.Layout.Column = 1; app.PassLabel.Text = 'Password:'; % Create PassEditField app.PassEditField = uieditfield(app.GridLayout, 'password'); app.PassEditField.Layout.Row = 3; app.PassEditField.Layout.Column = 2; % Create LoginButton app.LoginButton = uibutton(app.GridLayout, 'push'); app.LoginButton.Layout.Row = 4; app.LoginButton.Layout.Column = [1 2]; app.LoginButton.Text = 'Login'; app.LoginButton.ButtonPushedFcn = createCallbackFcn(app, @LoginButtonPushed, true); end end % App creation and deletion methods (Access = public) % Construct app function app = LoginUI % Create UIFigure and components createComponents(app); % Show the figure after all components are created app.UIFigure.Visible = 'on'; end end end ``` 以上代码仅供参考,可以根据实际需求进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值