MATLAB appdesigner GUI设计学习——基础部分

参考链接:https://www.bilibili.com/video/BV16f4y147x9/?spm_id_from=333.999.0.0

一、控制信号灯

    % Callbacks that handle component events
    methods (Access = private)

        % Value changed function: Switch
        function SwitchValueChanged(app, event)
            value = app.Switch.Value;
            
            %if条件判断
            if value == "On"
                app.Lamp.Color = "1.00,0.00,0.00"
            else
                app.Lamp.Color = "0.00,1.00,0.00"
            end
        end
    end

在这里插入图片描述

二、按条件控制信号灯

        % Value changed function: temp_value
        function temp_valueValueChanged(app, event)
            %定义环境温度
            value = app.temp_value.Value;
            
            if value>= 500
                app.Lamp.Color="1.00,0.00,0.00";
                app.show_tips.Value = "温度过高";
            elseif value < 500 && value>=400
                app.Lamp.Color="1.00,0.41,0.16";
                app.show_tips.Value = "温度适中";
            else
                app.Lamp.Color="0.30,0.75,0.93";
                app.show_tips.Value = "温度过低";
            end
        end
    end

在这里插入图片描述

三、显示图像的回调函数

            % 读入一张图片
            file = "D:\MATLAB files\GUI learn\xiye.jpeg";
            im = imread(file,'jpeg');
            imshow(im,'Parent',app.UIAxes)
            axis(app.UIAxes,'off')

四、点击按钮显示对话框

在这里插入图片描述
五、提问对话框

在这里插入图片描述

            choice = questdlg("您要关闭吗?","关闭","Yes","No","No");
            switch choice
                case "Yes"
                    delete(app.UIFigure); % close all window
                    return;  % 返回
                case "No"
                    return;
            end

在这里插入图片描述
六、交互性操作获得文件

在这里插入图片描述

在这里插入图片描述

            [filename,pathname] = uigetfile({'*.jpg';'*.jpeg';'*.png';'*.*'},'选择图像');
            if isequal(filename,0) || isequal(pathname,0)  %判断两个值是否相等
                errordlg("没有选中文件","错误");
            else
                %记录获取文件的名字和路径
                file = strcat(pathname,filename);
                
            end

在这里插入图片描述

            [filename,pathname] = uigetfile({'*.jpg';'*.jpeg';'*.png';'*.*'},'选择图像');
            if isequal(filename,0) || isequal(pathname,0)  %判断两个值是否相等
                errordlg("没有选中文件","错误");
            else
                %记录获取文件的名字和路径
                file = strcat(pathname,filename);   
            end
            % 原图
            im = imread(file);
            imshow(im,'Parent',app.UIAxes);
            msgbox("读取成功","提示");
            
            % 灰度图
            gray_image = rgb2gray(im);
            imshow(gray_image,'Parent',app.UIAxes2);

在这里插入图片描述
七、让界面一开始就显示东西

在这里插入图片描述

        function startupFcn(app)
            im = imread("D:\MATLAB files\GUI learn\start.jpeg");
            imshow(im,'Parent',app.UIAxes);
        end

在这里插入图片描述
八、添加函数

在这里插入图片描述

    methods (Access = private)
        
        function image_show(app)
            im = imread("D:\MATLAB files\GUI learn\start.jpeg");
            imshow(im,'Parent',app.UIAxes);
            
        end
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            image_show(app);
        end
    end

九、仪表盘

在这里插入图片描述

旋钮的changing方式,鼠标旋转到哪儿,示数到哪儿

在这里插入图片描述
changed模式,释放鼠标后,才会示值。
在这里插入图片描述
这里的仪表和旋钮不同,仅是显示控件,没有回调函数。

十、强制类型转换

在这里插入图片描述

在这里插入图片描述

    % Callbacks that handle component events
    methods (Access = private)

        % Value changed function: DatePicker
        function DatePickerValueChanged(app, event)
            value = app.DatePicker.Value;
            % 开始赋值
            value = string(value); %强制转换
            app.TextArea.Value = value;
        end
    end

在这里插入图片描述
十一、下拉菜单

    methods (Access = private)
        
        function func(app,value)
            x = 1:0.1:10;
            if value == "Option 1"
                y = sin(x);
            elseif value == "Option 2"
                y = cos(x);
            else
                return;
            end
            plot(app.UIAxes,x,y)
        end
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Button down function: UIAxes
        function UIAxesButtonDown(app, event)

        end

        % Value changed function: DropDown
        function DropDownValueChanged(app, event)
            value = app.DropDown.Value;
            
            if value == "Option 1"
                func(app,value);
            elseif value == "Option 2"
                func(app,value);
            else
                return;
            end
        end
    end

在这里插入图片描述

在这里插入图片描述
十三、不把参数写死,可以调整

    methods (Access = private)

        % Value changing function: Spinner
        function SpinnerValueChanging(app, event)
            changingValue = event.Value;
            app.Gauge.Value = changingValue;
            
            x = 0:0.1:changingValue;
            y = sin(x);
            plot(app.UIAxes,x,y,'r');
        end
    end

在这里插入图片描述
十四、单选按钮

    % Callbacks that handle component events
    methods (Access = private)

        % Selection changed function: ButtonGroup
        function ButtonGroupSelectionChanged(app, event)
            selectedButton = app.ButtonGroup.SelectedObject;
            
            switch selectedButton.Text
                case '1'
                    app.Lamp.Color = 'g';
                    app.Gauge.Value = 1;
                case '2'
                    app.Lamp.Color = 'r';
                    app.Gauge.Value = 2;
                otherwise
                    app.Lamp.Color = 'g';
                    app.Gauge.Value = 0;      
            end
        end
    end

在这里插入图片描述
十五、复选框

    methods (Access = private)

        % Value changed function: CheckBox
        function CheckBoxValueChanged(app, event)
            value = app.CheckBox.Value;
            
            % 判断类型
            app.EditField.Value = class(value);  % 布尔类型
            
            if value == true
                app.Lamp.Color = 'r';
            elseif value == false
                app.Lamp.Color = 'b';
            else
                app.Lamp.Color = 'g';
            end
        end
    end

在这里插入图片描述
十六、读取EXCEL表格

    % Callbacks that handle component events
    methods (Access = private)

        % Button pushed function: Button
        function ButtonPushed(app, event)
            
            %读取表格
            t = readtable("D:\transmittance_Si.xls");
            app.UITable.Data = t;
            
            % 把表头复制上去
            app.UITable.ColumnName = t.Properties.VariableNames;
        end
    end

在这里插入图片描述

并绘图

    % Callbacks that handle component events
    methods (Access = private)

        % Button pushed function: Button
        function ButtonPushed(app, event)
            
            %读取表格
            t = readtable("D:\transmittance_Si.xls");
            app.UITable.Data = t;
            
            % 显示表头
            %app.UITable.ColumnName = t.Properties.VariableNames; % 显示原有表头
            app.EditField.Value = t.Properties.VariableNames{1};
            app.EditField_2.Value = t.Properties.VariableNames{2};
            
            % 修改表头
            %app.UITable.ColumnName = {'x','y'};  % cell类型
             t.Properties.VariableNames{1} = 'x';
             t.Properties.VariableNames{2} = 'y';

            
            % 绘图
            x = t.x;
            y = t.y;
            plot(app.UIAxes,x,y,'r')
            
        end
    end

在这里插入图片描述
十七、增加表格

    methods (Access = private)

        % Button pushed function: add
        function addPushed(app, event)
            
            data1 = app.EditField.Value;
            price = app.EditField_2.Value;
            
            nr = {data1 price}; % 组合成cell类型
            app.UITable.Data = [app.UITable.Data;nr];
            
        end
    end

在这里插入图片描述
十八、表格删除

    % Callbacks that handle component events
    methods (Access = private)

        % Button pushed function: Button
        function ButtonPushed(app, event)
            t = readtable("D:\MATLAB files\Micropolarizer array\FDTD\transmittance_Si.xls");
            app.UITable.Data = t;
        end

        % Button pushed function: Button_2
        function Button_2Pushed(app, event)
            %(a,b) a代表行,b代表列
            row_n = app.EditField.Value;
            app.UITable.Data(row_n,:) = [];
        end
    end

在这里插入图片描述

  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值