Matlab之App Designer 如何导入Excel参数教程

一、背景介绍

       采用Matlab中的App Designer进行GUI可视界面程序设计时,经常需要将数据导入到程序中进行运算和分析,以提升效率,本文将对导入Excel参数的方法及程序实现进行介,。

二、采用Matlab App Designer导入Excel参数方法及代码

1、首先打开一个新的App Designer页面,并保存为Excel_input_app.mlapp,并在设计视图界面上拖入“编辑字段(文本)”组件,并对组件的属性进行修改,名称改为“参数输入”,如下图所示。

2、选择“Button按钮”组件拖入设计视图,并在组件属性中将组件按钮更改为“加载”,如 下图所示。

3、在设计视图界面上拖入“编辑字段(数值)”组件,同时拖入“Button按钮”组件,并对组件的属性进行修改,将“编辑字段(数值)”组件名称分别改为“弹簧刚度”,“弹簧预压值”,“弹簧变形量”,将“Button按钮”组件名称修改为“分析”,如下图所示。

4、对“加载”按钮组件添加回调,并进入代码视图。

5、进行导入Excel参数的代码编写,如下图所示

         clc
           [filename,pathname]=uigetfile({'*.xls';'*.xlsx';'*.*'},'File selector');
           pathname_EditField=string(pathname);
           filename_EditField=string(filename);
           if ~isequal(filename,0)
               app.EditField.Value=fullfile(pathname,filename);
           else
               return
           end
           filename_Ed=app.EditField.Value;
           %Excel中的数据导入,并转化成数组
           pat_1=readtable(filename_Ed,'Sheet',1);
           app.KEditField.Value=table2array(pat_1(1,2));
           app.L1EditField.Value=table2array(pat_1(2,2));
           app.LEditField.Value=table2array(pat_1(3,2));
           %将数据体现在编辑字段组件内

Excel参数表如下图所示,需注意的是,因为第1行为参数名称行,将第2行作为参数的识别行

5、对分析按钮添加回调,并进入代码视图,如下图所示。

6、进行分析的代码编写,如 下图所示

          K=app.KEditField.Value;
          L1=app.L1EditField.Value;
          L=app.L1EditField.Value;
          F=K*(L+L1);
          app.FEditField.Value=F;

7、进行运行,导入EXCEL参数表,并进行分析,参数输入的组件中显示导入的EXCEL表名称“弹簧参数”,同时弹簧刚度、弹簧预压值、弹簧变形量组件中显示的数值与Excel中的表中的数值相符。详细内容如下图所示。

8、点击分析,得到的分析结果如下图所示,弹簧力组件中即根据代码进行计算。

9、基于App Designer进行Excel参数导入及分析大详细代码如下。

classdef Excel_input_app_1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure           matlab.ui.Figure
        Button             matlab.ui.control.Button
        Button_2           matlab.ui.control.Button
        EditFieldLabel     matlab.ui.control.Label
        EditField          matlab.ui.control.EditField
        NmmEditFieldLabel  matlab.ui.control.Label
        KEditField         matlab.ui.control.NumericEditField
        mmEditFieldLabel   matlab.ui.control.Label
        L1EditField        matlab.ui.control.NumericEditField
        mmLabel            matlab.ui.control.Label
        LEditField         matlab.ui.control.NumericEditField
        Label              matlab.ui.control.Label
        FEditField         matlab.ui.control.NumericEditField
    end

    methods (Access = private)

        % Button pushed function: Button
        function ButtonPushed(app, event)
           clc
           [filename,pathname]=uigetfile({'*.xls';'*.xlsx';'*.*'},'File selector');
           pathname_EditField=string(pathname);
           filename_EditField=string(filename);
           if ~isequal(filename,0)
               app.EditField.Value=fullfile(pathname,filename);
           else
               return
           end
           filename_Ed=app.EditField.Value;
           %Excel中的数据导入,并转化成数组
           pat_1=readtable(filename_Ed,'Sheet',1);
           app.KEditField.Value=table2array(pat_1(1,2));
           app.L1EditField.Value=table2array(pat_1(2,2));
           app.LEditField.Value=table2array(pat_1(3,2));
           %将数据体现在编辑字段组件内
        end

        % Button pushed function: Button_2
        function Button_2Pushed(app, event)
          K=app.KEditField.Value;
          L1=app.L1EditField.Value;
          L=app.LEditField.Value;
          F=K*(L+L1);
          app.FEditField.Value=F;
        
        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 640 480];
            app.UIFigure.Name = 'UI Figure';

            % Create Button
            app.Button = uibutton(app.UIFigure, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.Position = [512 391 100 24];
            app.Button.Text = '加载';

            % Create Button_2
            app.Button_2 = uibutton(app.UIFigure, 'push');
            app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true);
            app.Button_2.Position = [125 192 100 24];
            app.Button_2.Text = '分析';

            % Create EditFieldLabel
            app.EditFieldLabel = uilabel(app.UIFigure);
            app.EditFieldLabel.HorizontalAlignment = 'right';
            app.EditFieldLabel.Position = [40 393 53 22];
            app.EditFieldLabel.Text = '参数输入';

            % Create EditField
            app.EditField = uieditfield(app.UIFigure, 'text');
            app.EditField.Position = [108 393 282 22];

            % Create NmmEditFieldLabel
            app.NmmEditFieldLabel = uilabel(app.UIFigure);
            app.NmmEditFieldLabel.HorizontalAlignment = 'right';
            app.NmmEditFieldLabel.Position = [17 300 93 22];
            app.NmmEditFieldLabel.Text = '弹簧刚度(N/mm)';

            % Create KEditField
            app.KEditField = uieditfield(app.UIFigure, 'numeric');
            app.KEditField.Position = [125 300 100 22];

            % Create mmEditFieldLabel
            app.mmEditFieldLabel = uilabel(app.UIFigure);
            app.mmEditFieldLabel.HorizontalAlignment = 'right';
            app.mmEditFieldLabel.Position = [305 300 93 22];
            app.mmEditFieldLabel.Text = '弹簧预压值(mm)';

            % Create L1EditField
            app.L1EditField = uieditfield(app.UIFigure, 'numeric');
            app.L1EditField.Position = [413 300 100 22];

            % Create mmLabel
            app.mmLabel = uilabel(app.UIFigure);
            app.mmLabel.HorizontalAlignment = 'right';
            app.mmLabel.Position = [17 246 93 22];
            app.mmLabel.Text = '弹簧变形量(mm)';

            % Create LEditField
            app.LEditField = uieditfield(app.UIFigure, 'numeric');
            app.LEditField.Position = [125 246 100 22];

            % Create Label
            app.Label = uilabel(app.UIFigure);
            app.Label.HorizontalAlignment = 'right';
            app.Label.Position = [305 194 41 22];
            app.Label.Text = '弹簧力';

            % Create FEditField
            app.FEditField = uieditfield(app.UIFigure, 'numeric');
            app.FEditField.Position = [361 194 100 22];
        end
    end

    methods (Access = public)

        % Construct app
        function app = Excel_input_app_1

            % Create and configure components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end

三、总结

基于Matlab App Designer进行Excel参数导入,可以方便用户快速进行参数的分析,提升分析效率,并且在页面上增加编辑字段的数值显示,同时支持在线修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值