Mtalab的GUI设置的简单示例

一、计算诊断解锁密钥的GUI示意图

在这里插入图片描述

1、作用

可以根据解锁等级来计算相应的key

2、注意点

1)解锁等级的互斥
2)未选解锁等级时的弹窗提醒
3)输入key的不合法报错(try, catch, retun结合使用)

3、代码

classdef MainGui < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                   matlab.ui.Figure
        Level1CheckBox             matlab.ui.control.CheckBox
        Button                     matlab.ui.control.Button
        Level3CheckBox             matlab.ui.control.CheckBox
        Level11CheckBox            matlab.ui.control.CheckBox
        DemoKey0EditFieldLabel     matlab.ui.control.Label
        DemoKey0EditField          matlab.ui.control.EditField
        ReceivedKeyEditFieldLabel  matlab.ui.control.Label
        ReceivedKeyEditField       matlab.ui.control.EditField
        DemoKey1EditFieldLabel     matlab.ui.control.Label
        DemoKey1EditField          matlab.ui.control.EditField
        DemoKey2EditFieldLabel     matlab.ui.control.Label
        DemoKey2EditField          matlab.ui.control.EditField
        DemoKey3EditFieldLabel     matlab.ui.control.Label
        DemoKey3EditField          matlab.ui.control.EditField
    end

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

        % Button pushed function: Button
        function ButtonPushed(app, event)
            fprintf("DemoKey's Value = %d\n", app.DemoKey0EditField.Value);

            Value_Temp = (bitshift(double(app.Level1CheckBox.Value), 0)+...
                bitshift(double(app.Level3CheckBox.Value), 1)+...
                bitshift(double(app.Level11CheckBox.Value), 2));%Calculate Level
            
                
            Temp_u_CalXorTb = [hex2dec("65"), hex2dec("67"), ...
                               hex2dec("77"), hex2dec("E9")];
            try                     
                SecM_s_Seed = [hex2dec(app.DemoKey0EditField.Value),...
                                hex2dec(app.DemoKey1EditField.Value), ...
                                hex2dec(app.DemoKey2EditField.Value),...
                                hex2dec(app.DemoKey3EditField.Value)];
            catch
%                 SecM_s_Seed = ["0", "0", "0", "0"];
                errordlg('The enter key is out range !!!','error');
                return;%防止程序出错时向下运行
            end
            
            Temp_u_Buf = [0, 0, 0, 0];
            SecM_s_Key = [0, 0, 0, 0];
            
                if(Value_Temp==1)
                    for a_index = 1:4
                        %fprintf("ff=%d\n",SecM_s_Seed(a_index));
                        Temp_u_Buf(a_index) = bitxor(SecM_s_Seed(a_index), ...
                            Temp_u_CalXorTb(a_index));%Yi Huo
                    end
                    SecM_s_Key(1) = bitshift(bitand(Temp_u_Buf(4), hex2dec("0F")), 4);
                    SecM_s_Key(1) = bitor( SecM_s_Key(1), bitand(Temp_u_Buf(4), hex2dec("F0")) );
                    
                    SecM_s_Key(2) = bitshift(bitand(Temp_u_Buf(2), hex2dec("0F")), 4);
                    SecM_s_Key(2) = bitor( SecM_s_Key(2), bitshift(bitand(Temp_u_Buf(1), hex2dec("F0")), -4) );

                    SecM_s_Key(3) = bitshift(bitand(Temp_u_Buf(2), hex2dec("F0")), 0);
                    SecM_s_Key(3) = bitor( SecM_s_Key(3), bitshift(bitand(Temp_u_Buf(3), hex2dec("F0")), -4) );

                    SecM_s_Key(4) = bitshift(bitand(Temp_u_Buf(1), hex2dec("0F")), 4);
                    SecM_s_Key(4) = bitor( SecM_s_Key(4), bitshift(bitand(Temp_u_Buf(3), hex2dec("0F")), 0) );
                    
                    app.ReceivedKeyEditField.Value = strcat(...
                        strcat( strcat(dec2hex(SecM_s_Key(1)), dec2hex(SecM_s_Key(2))), dec2hex(SecM_s_Key(3)) ),...
                        dec2hex(SecM_s_Key(4)));
                
                elseif(Value_Temp==2)
                    app.ReceivedKeyEditField.Value = num2str(2);
                elseif(Value_Temp==4)
                    app.ReceivedKeyEditField.Value = num2str(4);
                else
                    warndlg('Please choose the key level !!!','warn');
                end
            

        end

        % Value changed function: Level1CheckBox
        function Level1CheckBoxValueChanged(app, event)
            value = app.Level1CheckBox.Value;
            fprintf("HAHA = %d\n",value);
            if(value == 1)
                app.Level3CheckBox.Enable = false;
                app.Level11CheckBox.Enable = false;
            elseif(value == 0)
                app.Level3CheckBox.Enable = true;
                app.Level11CheckBox.Enable = true;
            end
            
        
        end

        % Value changed function: Level11CheckBox
        function Level11CheckBoxValueChanged(app, event)
            value = app.Level11CheckBox.Value;
            if(value == 1)
                app.Level1CheckBox.Enable = false;
                app.Level3CheckBox.Enable = false;
            elseif(value == 0)
                app.Level1CheckBox.Enable = true;
                app.Level3CheckBox.Enable = true;
            end
        end

        % Value changed function: Level3CheckBox
        function Level3CheckBoxValueChanged(app, event)
            value = app.Level3CheckBox.Value;
            if(value == 1)
                app.Level1CheckBox.Enable = false;
                app.Level11CheckBox.Enable = false;
            elseif(value == 0)
                app.Level1CheckBox.Enable = true;
                app.Level11CheckBox.Enable = true;
            end
            
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'UI Figure';

            % Create Level1CheckBox
            app.Level1CheckBox = uicheckbox(app.UIFigure);
            app.Level1CheckBox.ValueChangedFcn = createCallbackFcn(app, @Level1CheckBoxValueChanged, true);
            app.Level1CheckBox.Text = 'Level 1';
            app.Level1CheckBox.Position = [96 398 61 22];

            % Create Button
            app.Button = uibutton(app.UIFigure, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.Position = [91 119 100 22];

            % Create Level3CheckBox
            app.Level3CheckBox = uicheckbox(app.UIFigure);
            app.Level3CheckBox.ValueChangedFcn = createCallbackFcn(app, @Level3CheckBoxValueChanged, true);
            app.Level3CheckBox.Text = 'Level 3';
            app.Level3CheckBox.Position = [191 398 61 22];

            % Create Level11CheckBox
            app.Level11CheckBox = uicheckbox(app.UIFigure);
            app.Level11CheckBox.ValueChangedFcn = createCallbackFcn(app, @Level11CheckBoxValueChanged, true);
            app.Level11CheckBox.Text = 'Level 11';
            app.Level11CheckBox.Position = [284 398 66 22];

            % Create DemoKey0EditFieldLabel
            app.DemoKey0EditFieldLabel = uilabel(app.UIFigure);
            app.DemoKey0EditFieldLabel.HorizontalAlignment = 'right';
            app.DemoKey0EditFieldLabel.Position = [90 329 68 22];
            app.DemoKey0EditFieldLabel.Text = 'Demo Key0';

            % Create DemoKey0EditField
            app.DemoKey0EditField = uieditfield(app.UIFigure, 'text');
            app.DemoKey0EditField.Position = [173 329 100 22];
            app.DemoKey0EditField.Value = '21';

            % Create ReceivedKeyEditFieldLabel
            app.ReceivedKeyEditFieldLabel = uilabel(app.UIFigure);
            app.ReceivedKeyEditFieldLabel.HorizontalAlignment = 'right';
            app.ReceivedKeyEditFieldLabel.Position = [91 199 80 22];
            app.ReceivedKeyEditFieldLabel.Text = 'Received Key';

            % Create ReceivedKeyEditField
            app.ReceivedKeyEditField = uieditfield(app.UIFigure, 'text');
            app.ReceivedKeyEditField.Position = [191 199 275 22];

            % Create DemoKey1EditFieldLabel
            app.DemoKey1EditFieldLabel = uilabel(app.UIFigure);
            app.DemoKey1EditFieldLabel.HorizontalAlignment = 'right';
            app.DemoKey1EditFieldLabel.Position = [284 329 68 22];
            app.DemoKey1EditFieldLabel.Text = 'Demo Key1';

            % Create DemoKey1EditField
            app.DemoKey1EditField = uieditfield(app.UIFigure, 'text');
            app.DemoKey1EditField.Position = [367 329 100 22];
            app.DemoKey1EditField.Value = '25';

            % Create DemoKey2EditFieldLabel
            app.DemoKey2EditFieldLabel = uilabel(app.UIFigure);
            app.DemoKey2EditFieldLabel.HorizontalAlignment = 'right';
            app.DemoKey2EditFieldLabel.Position = [91 279 68 22];
            app.DemoKey2EditFieldLabel.Text = 'Demo Key2';

            % Create DemoKey2EditField
            app.DemoKey2EditField = uieditfield(app.UIFigure, 'text');
            app.DemoKey2EditField.Position = [174 279 100 22];
            app.DemoKey2EditField.Value = '27';

            % Create DemoKey3EditFieldLabel
            app.DemoKey3EditFieldLabel = uilabel(app.UIFigure);
            app.DemoKey3EditFieldLabel.HorizontalAlignment = 'right';
            app.DemoKey3EditFieldLabel.Position = [284 277 68 22];
            app.DemoKey3EditFieldLabel.Text = 'Demo Key3';

            % Create DemoKey3EditField
            app.DemoKey3EditField = uieditfield(app.UIFigure, 'text');
            app.DemoKey3EditField.Position = [367 277 100 22];
            app.DemoKey3EditField.Value = '3D';

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = MainGui

            % Create UIFigure and 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的GUI代码中,很多代码是自动生成的,也无法修改,只能修改组件的回调函数。

二、生成随机数(点名)GUI示意图

在这里插入图片描述

1、作用

可以用于基于学号的点名

2、注意点

1)Mtalab GUI全局变量的设置
2)列向量的赋值

3、代码

classdef ChooseLuckyDog < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                   matlab.ui.Figure
        arraylengthEditFieldLabel  matlab.ui.control.Label
        arraylengthEditField       matlab.ui.control.EditField
        studentnumbermaxLabel      matlab.ui.control.Label
        studentnumbermaxEditField  matlab.ui.control.EditField
        luckydogEditFieldLabel     matlab.ui.control.Label
        luckydogEditField          matlab.ui.control.EditField
        Button                     matlab.ui.control.Button
        UITable                    matlab.ui.control.Table
        historyindexarraylengthEditFieldLabel  matlab.ui.control.Label
        historyindexarraylengthEditField  matlab.ui.control.EditField
    end

    
    properties (Access = public)
        hoisdwoh = 0;%the global variable
    end

    

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

        % Button pushed function: Button
        function ButtonPushed(app, event)
        
            if(str2double(app.studentnumbermaxEditField.Value)<=...
                    str2double(app.historyindexarraylengthEditField.Value))
                errordlg('The enter parameter is abnormal !!!','error');
                return;
                
            end
        
        
            sortArray = sort(round(rand(1,str2double(app.arraylengthEditField.Value))*...
                str2double(app.studentnumbermaxEditField.Value)));

            if(str2double(app.arraylengthEditField.Value) <= 0)
                errordlg('The enter parameter is wrong !!!','error');
                return;
            else
                app.luckydogEditField.Value =num2str( sortArray( randi(str2double(app.arraylengthEditField.Value)) ) );%(double(round(rand(1,1)*inputArg1)));
            end
            
            while(ismember(str2double( app.luckydogEditField.Value ),app.hoisdwoh))
                
                fprintf("recall it = %d\n", str2double( app.luckydogEditField.Value));
                
                
                sortArray = sort(round(rand(1,str2double(app.arraylengthEditField.Value))*...
                    str2double(app.studentnumbermaxEditField.Value)));
    
                if(str2double(app.arraylengthEditField.Value) <= 0)
                    errordlg('The enter parameter is wrong !!!','error');
                    return;
                else
                    app.luckydogEditField.Value =num2str( sortArray( randi(str2double(app.arraylengthEditField.Value)) ) );%(double(round(rand(1,1)*inputArg1)));
                end
                
            end
            
            

            if(0 == app.hoisdwoh)
                app.hoisdwoh = zeros(str2double( app.historyindexarraylengthEditField.Value) );%number of students
                fprintf("eneneneen\n");
                for i = 1:length(app.hoisdwoh)
                    app.hoisdwoh(i) = str2double( app.luckydogEditField.Value );
                end
            end
            
            for x = 1:length(app.hoisdwoh)

                if(x <= length(app.hoisdwoh)-1)
                    app.UITable.Data(x,1) = app.hoisdwoh(x);
                else
                    app.UITable.Data(x) = str2double( app.luckydogEditField.Value );
                end
                
            end

            for x = 1:length(app.hoisdwoh)-1
                app.hoisdwoh(x)=app.UITable.Data(x+1);%shift array members
            end
            
            
            
            
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            delete(app)
            fprintf("close the app\n");
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'UI Figure';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);

            % Create arraylengthEditFieldLabel
            app.arraylengthEditFieldLabel = uilabel(app.UIFigure);
            app.arraylengthEditFieldLabel.HorizontalAlignment = 'right';
            app.arraylengthEditFieldLabel.Position = [75 331 76 22];
            app.arraylengthEditFieldLabel.Text = 'array length: ';

            % Create arraylengthEditField
            app.arraylengthEditField = uieditfield(app.UIFigure, 'text');
            app.arraylengthEditField.Position = [166 331 100 22];
            app.arraylengthEditField.Value = '5';

            % Create studentnumbermaxLabel
            app.studentnumbermaxLabel = uilabel(app.UIFigure);
            app.studentnumbermaxLabel.HorizontalAlignment = 'right';
            app.studentnumbermaxLabel.Position = [318 331 122 22];
            app.studentnumbermaxLabel.Text = 'student number max: ';

            % Create studentnumbermaxEditField
            app.studentnumbermaxEditField = uieditfield(app.UIFigure, 'text');
            app.studentnumbermaxEditField.Position = [448 331 100 22];
            app.studentnumbermaxEditField.Value = '35';

            % Create luckydogEditFieldLabel
            app.luckydogEditFieldLabel = uilabel(app.UIFigure);
            app.luckydogEditFieldLabel.HorizontalAlignment = 'right';
            app.luckydogEditFieldLabel.Position = [74 257 63 22];
            app.luckydogEditFieldLabel.Text = 'lucky dog: ';

            % Create luckydogEditField
            app.luckydogEditField = uieditfield(app.UIFigure, 'text');
            app.luckydogEditField.Position = [166 257 100 22];

            % Create Button
            app.Button = uibutton(app.UIFigure, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.Position = [75 162 100 22];

            % Create UITable
            app.UITable = uitable(app.UIFigure);
            app.UITable.ColumnName = {'Column 1'};
            app.UITable.RowName = {};
            app.UITable.Position = [285 60 263 185];

            % Create historyindexarraylengthEditFieldLabel
            app.historyindexarraylengthEditFieldLabel = uilabel(app.UIFigure);
            app.historyindexarraylengthEditFieldLabel.HorizontalAlignment = 'right';
            app.historyindexarraylengthEditFieldLabel.Position = [285 257 155 22];
            app.historyindexarraylengthEditFieldLabel.Text = 'history index array length: ';

            % Create historyindexarraylengthEditField
            app.historyindexarraylengthEditField = uieditfield(app.UIFigure, 'text');
            app.historyindexarraylengthEditField.Position = [448 257 100 22];
            app.historyindexarraylengthEditField.Value = '7';

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = ChooseLuckyDog

            % Create UIFigure and 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
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值