MATLAB app designer学习记录

记录一些学习心得,以便查看。

msgbox

msgbox(‘显示内容’,‘标题’)

msgbox(‘显示内容’,‘标题’,‘系统图标’),系统图标主要有none,error,help,warn

自定义图标
[IconData,IconCMap]=imread(图像名)
msgbox(‘显示内容’,‘标题’,‘custom’,‘IconData’,‘IconCMap’)

采用元胞数组来显示多行文字
h=msgbox({‘您所运算的结果为:’,‘…’,‘Radius=25’,‘Area=19.635’,…
‘…’},‘Comupute result’,‘help’);

questdlg

问题对话框,其基本用法为

button = questdlg(qstring,title,str1,str2,default);

qstring为对话框显示的内容
itle为标题
str1和str2为选项
default为默认选项
button为返回值

uialert

uialert(fig,message,title,‘Icon’,icon_path);

uigetfile

创建标准的对话框并通过交互式操作取得文件名

[FileName,PathName,FilterIndex] = uigetfile(FilterSpec,DialogTitle,DefaultName);

FileName:返回的文件名
PathName:返回的文件的路径名
FilterIndex:选择的文件类型
FilterSpec:文件类型设置
DialogTitle:打开对话框的标题
DefaultName:默认指向的文件名
注意:记得判断返回值是否为空。

UIAxes的绘图

x=0:0.01:10;
y=sin(x);
plot(app.UIAxes,x,y,"r");%r颜色
hold(app.UIAxes,"on");%同一坐标显示多个

UIAxes的图像显示

file="D:\myfiles\labi.jpg";
im = imread(file);
imshow(im,'Parent',app.UIAxes);

axis(app.UIAxes,'off');%隐藏坐标轴

鼠标键盘

UIFigure 的 WindowButtonDownFcn 函数是当鼠标在 UIFigure 中点击时(无论是左键还是右键),程序调用的回调函数。
而 ButtonDownFcn 则是当控件位于该控件的区域内时,点击时所调用的函数。

A=app.UIFigure.CurrentPoint ; %获取鼠标位置,x坐标A(1,1),y坐标A(1,2)。
key = event.Key; %获取按键值

% 判断鼠标点击类型 是否为 左键,返回 1 或者 0
tf = strcmp(app.UIFigure.SelectionType, 'normal')  %  strcmp 比较俩字符串

normal:代表单击鼠标左键;
extend:代表Shift+左键,或者同时按左右键;
alt:代表Ctrl+左键,或者单击右键;
open:代表双击鼠标任意键。

定时器demo

classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure  matlab.ui.Figure
        per       matlab.ui.control.Spinner
        Label     matlab.ui.control.Label
        set       matlab.ui.control.Button
        show      matlab.ui.control.NumericEditField
        stop      matlab.ui.control.Button
        start     matlab.ui.control.Button
    end

    properties (Access = private)
        Timer_id;
    end

    methods (Access = private)
        % 定时器初始化
        function timer_init(app)
            app.Timer_id = timer;
            app.Timer_id.StartDelay = 0.01; % 开始定时器时与启动倒计时之间的间隔。
            app.Timer_id.Period = 1.0; % 定时器定时的周期。
            app.Timer_id.ExecutionMode = 'fixedSpacing';%指定定时器的触发方式,有四种
            % sigleShot;只执行一次,执行后会自动关闭定时器。其他模式可以循环执行
            % fixedDelay;上一次TimerFcn执行完毕时刻到下一次TimerFcn被加入队列时刻之间的间隔
            % fixedRate;上一次开始执行到下一次被加入队列之间的间隔
            % fixedSpacing: 前后两次被加入到执行语句队列时刻之间的间隔
            app.Timer_id.TimerFcn = @(~, ~) timer_handler(app);
        end

        % 定时器启动
        function timer_start(app)
            try
                start(app.Timer_id);    %定时器正在启动时则无法运行
            catch
            end
        end

        % 定时停止
        function timer_stop(app)
            stop(app.Timer_id);
        end

        % 删除定时器
        % 要在定时器停止后,才能进行删除或修改周期!!!
        function timer_delete(app)
            delete(app.Timer_id);
        end

        % 定时器回调
        function timer_handler(app)
            %执行定时器任务
            app.show.Value = app.show.Value +1;
        end

    end


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

        % Code that executes after component creation
        function startupFcn(app)
            app.show.Value = 0;
            timer_init(app);
        end

        % Button pushed function: set
        function setButtonPushed(app, event)
            timer_stop(app);
            app.Timer_id.Period = app.per.Value; % Period必须为正值
            timer_start(app);
        end

        % Button pushed function: start
        function startButtonPushed(app, event)
            timer_start(app);
        end

        % Button pushed function: stop
        function stopButtonPushed(app, event)
            timer_stop(app);
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            % 停止定时器,然后删除
            timer_stop(app);
            timer_delete(app);
            delete(app)
        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 250 300];
            app.UIFigure.Name = 'MATLAB App';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);

            % Create start
            app.start = uibutton(app.UIFigure, 'push');
            app.start.ButtonPushedFcn = createCallbackFcn(app, @startButtonPushed, true);
            app.start.Position = [79 78 100 24];
            app.start.Text = '开始';

            % Create stop
            app.stop = uibutton(app.UIFigure, 'push');
            app.stop.ButtonPushedFcn = createCallbackFcn(app, @stopButtonPushed, true);
            app.stop.Position = [80 30 100 24];
            app.stop.Text = '停止';

            % Create show
            app.show = uieditfield(app.UIFigure, 'numeric');
            app.show.HorizontalAlignment = 'center';
            app.show.FontSize = 20;
            app.show.Position = [69 221 116 46];

            % Create set
            app.set = uibutton(app.UIFigure, 'push');
            app.set.ButtonPushedFcn = createCallbackFcn(app, @setButtonPushed, true);
            app.set.Position = [78 126 100 24];
            app.set.Text = '设置';

            % Create Label
            app.Label = uilabel(app.UIFigure);
            app.Label.HorizontalAlignment = 'right';
            app.Label.Position = [142 174 29 22];
            app.Label.Text = '周期';

            % Create per
            app.per = uispinner(app.UIFigure);
            app.per.Step = 0.1;
            app.per.Limits = [0.1 10];
            app.per.Position = [83 174 59 22];
            app.per.Value = 0.1;

            % 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 = app1

            % Create UIFigure and components
            createComponents(app)

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

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值