《又到毕业季》MATALB GUI 基础控件与交互

Posted 今天不飞了

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《又到毕业季》MATALB GUI 基础控件与交互相关的知识,希望对你有一定的参考价值。

《又到毕业季》MATALB GUI 基础控件与交互

眼看六月就要毕业答辩了,你的论文是不是还差一个GUI没有做呢,赶紧来学基础控件与交互吧。

by 今天不飞了


视频教学地址

《又到毕业季》MATLAB GUI(一)窗体与布局
《又到毕业季》MATLAB GUI(二)按钮
《又到毕业季》MATLAB GUI(三)单选复选
《又到毕业季》MATLAB GUI(四)下拉菜单
《又到毕业季》MATLAB GUI(五)列表
《又到毕业季》MATLAB GUI(六)滚动条
《又到毕业季》MATLAB GUI(七)文本编辑框
《又到毕业季》MATLAB GUI(八)接口与调用


一、窗体与排版

function GUI1()

Fig1 = figure('Position',[600,200,800,800],'menu','none');
Fig1.Color = 'white';
Fig1.NumberTitle = 'off';
Fig1.Name = 'GUI1';

Pnl1 = uipanel(Fig1,'Position',[0.1,0.5,0.8,0.45]);
Pnl2 = uipanel(Fig1,'Position',[0.1,0.05,0.8,0.45]);

Axes1 = axes(Pnl1,'Position',[0.1,0.1,0.4,0.8]);
Axes2 = axes(Pnl1,'Position',[0.5,0.1,0.4,0.8]);

Axes3 = axes(Pnl2,'Position',[0.1,0.1,0.4,0.8]);
Axes4 = axes(Pnl2,'Position',[0.5,0.1,0.4,0.8]);

x = 1:100;
y = x*2;
im = rand(100);
imrgb = rand(100,100,3);

plot(Axes1,x,y,'b')
plot(Axes2,x,y,'r')
imshow(im,'Parent',Axes3)
imshow(imrgb,'Parent',Axes4)

end

二、按钮

function GUI2()

Fig = figure('Position',[700,200,600,600],'menu','none');
Fig.Color = 'white';
Fig.NumberTitle = 'off';
Fig.Name = 'GUI2';

Pnl1 = uipanel(Fig,'Position',[0.1,0.17,0.8,0.8]);
Pnl2 = uipanel(Fig,'Position',[0.1,0.05,0.8,0.1]);

Axes = axes(Pnl1,'Position',[0,0,1,1]);

Bt1= uicontrol(Pnl2,'style','pushbutton','String','请解锁','Fontsize',16,...
    'Units','normalized','Position',[0,0,0.5,1],'Callback',@ChangeColor);
Bt2 = uicontrol(Pnl2,'style','togglebutton','String','解锁','Fontsize',16,...
    'Units','normalized','Position',[0.5,0,0.5,1],'Callback',@Lock);

state = 0;
im = ones(100,100,3);
imshow(im,'Parent',Axes)

    function ChangeColor(~,~)
        if state
            rgb = rand(1,1,3);
            imnew = im.*rgb;
            imshow(imnew,'Parent',Axes)
        end
    end

    function Lock(~,~)
        state = get(Bt2,'Value');
        if state
            set(Bt2,'String','锁定')
            set(Bt1,'String','更改颜色')
        else
            set(Bt2,'String','解锁')
            set(Bt1,'String','请解锁')
        end
    end

end

三、单选复选

function GUI3()

im = ones(100,100,3);

Fig = figure('Position',[500,200,900,600],'menu','none');
Fig.Color = 'white';
Fig.NumberTitle = 'off';
Fig.Name = 'GUI3';

% 绘图面板
Pnl1 = uipanel(Fig,'Position',[0.05,0.35,0.9,0.6],...
    'Title','绘图窗口','Fontsize',14);
% 选项面板框
Pnl2 = uipanel(Fig,'Position',[0.05,0.05,0.3,0.3],...
    'Title','复选框','Fontsize',14);
Pnl3 = uipanel(Fig,'Position',[0.35,0.05,0.3,0.3],...
    'Title','单选框','Fontsize',14);
% 按钮组
Pnl4 = uibuttongroup (Fig,'Position',[0.65,0.05,0.3,0.3],...
    'Title','单选框组','Fontsize',14);

Axes1 = axes(Pnl1,'Position',[0,0,1/3,1]);
Axes2 = axes(Pnl1,'Position',[1/3,0,1/3,1]);
Axes3 = axes(Pnl1,'Position',[2/3,0,1/3,1]);

checkBoxList = '红','绿','蓝';
colorList = [1,0.6,0.6],[0.6,1,0.6],[0.6,0.6,1];
BtGroup1 = cell(3,1);
for n = 1:3
    BtGroup1n = uicontrol(Pnl2,'style','checkbox',...
        'String',checkBoxListn,'Fontsize',16,'BackgroundColor',colorListn,...
        'Units','normalized','Position',[0.2,1-n/3,0.6,1/3],...
        'Callback',@ChangeColor1);
end

BtGroup2 = cell(3,1);
for n = 1:3
    BtGroup2n = uicontrol(Pnl3,'style','radiobutton',...
        'String',checkBoxListn,'Fontsize',16,'BackgroundColor',colorListn,...
        'Units','normalized','Position',[0.2,1-n/3,0.6,1/3],...
        'Callback',@ChangeColor2);
end

BtGroup3 = cell(3,1);
for n = 1:3
    BtGroup3n = uicontrol(Pnl4,'style','radiobutton',...
        'String',checkBoxListn,'Fontsize',16,'BackgroundColor',colorListn,...
        'Units','normalized','Position',[0.2,1-n/3,0.6,1/3],...
        'Callback',@ChangeColor3);
end

% 初始化
ChangeColor1()
ChangeColor2()
ChangeColor3()

    function ChangeColor1(~,~)
        rgb = zeros(1,1,3);
        for i = 1:3
            rgb(i) = get(BtGroup1i,'Value');
        end
        imnew = im.*rgb;
        imshow(imnew,'Parent',Axes1);
    end

    function ChangeColor2(~,~)
        rgb = zeros(1,1,3);
        for i = 1:3
            rgb(i) = get(BtGroup2i,'Value');
        end
        imnew = im.*rgb;
        imshow(imnew,'Parent',Axes2);
    end

    function ChangeColor3(~,~)
        rgb = zeros(1,1,3);
        for i = 1:3
            rgb(i) = get(BtGroup3i,'Value');
        end
        imnew = im.*rgb;
        imshow(imnew,'Parent',Axes3);
    end

end

四、下拉菜单

function GUI4()
im = imread('test.jpg');
Fig = figure('Position',[500,200,1400,800],'menu','none');
Fig.Color = 'white';
Fig.NumberTitle = 'off';
Fig.Name = 'GUI4';

% 面板
Pnl1 = uipanel(Fig,'Position',[0.1,0.25,0.8,0.7]);
Pnl2 = uipanel(Fig,'Position',[0.1,0.05,0.8,0.1]);

% 下拉按钮
sizeList = [1,1],[1,2],[2,2],[2,4];
for s = 1:length(sizeList)
    menuLists = strcat(num2str(sizeLists(1)),'*',num2str(sizeLists(2)));
end
Menu = uicontrol(Pnl2,'style','popupmenu',...
    'String',menuList,'Fontsize',16,...
    'Units','normalized','Position',[0,0,1,1],...
    'Callback',@GenerateAxes);

% 绘图窗初始化
Axes = [];
axesNum = 0;
GenerateAxes();

    function GenerateAxes(~,~)
        tmp = get(Menu,'Value');
        
        for n = 1:axesNum
           delete(Axesn) 
        end
        Axes = [];
        
        row = sizeListtmp(1);
        col = sizeListtmp(2);
        for r = 1:row
            for c = 1:col
                idx = (r-1)*col+c;
                Axesidx = axes(Pnl1,'Position',[(c-1)/col,1-r/row,1/col,1/row]);
            end
        end
        axesNum = length(Axes);
        updata();
    end

    function updata()
       for n = 1:axesNum
           imshow(im,'Parent',Axesn)
       end
    end

end

五、列表

function GUI5()

% 基本框架
Fig = figure('Position',[800,200,1000,800],'menu','none');

Pnl1 = uipanel(Fig,'Position',[0.1,0.1,0.2,0.8]);
Pnl2 = uipanel(Fig,'Position',[0.3,0.1,0.6,0.8]);

Bt = uicontrol(Pnl1,'style','pushbutton',...
    'String','选择数据集','Fontsize',16,...
    'Unit','normalized','Position',[0,0.9,1,0.1],...
    'Callback',@LoadData);

Lb = uicontrol(Pnl1,'style','listbox',...
    'String',[],'Fontsize',12,...
    'Unit',以上是关于《又到毕业季》MATALB GUI 基础控件与交互的主要内容,如果未能解决你的问题,请参考以下文章

毕业季|进击的技术er又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说

又到了毕业迷茫期

毕业季最青春——致我们四年的美好时光,继续满怀期待奔向远方!

2022年毕业季 游戏本电脑哪个牌子好?

毕业季Java面试必备,阿里华为腾讯等公司面试题汇总

毕业季:应届程序员求职该做哪些准备?有哪些面试必备小技巧?