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

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

  • 视频教学地址
  • 一、窗体与排版
  • 二、按钮
  • 三、单选复选
  • 四、下拉菜单
  • 五、列表
  • 六、滚动条
  • 七、文本与编辑
  • 八、接口与调用
  • 其他

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

by 今天不飞了


视频教学地址

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


一、窗体与排版

《又到毕业季》MATALB GUI 基础控件与交互_第1张图片

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

二、按钮

《又到毕业季》MATALB GUI 基础控件与交互_第2张图片

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

三、单选复选

《又到毕业季》MATALB GUI 基础控件与交互_第3张图片

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
    BtGroup1{n} = uicontrol(Pnl2,'style','checkbox',...
        'String',checkBoxList{n},'Fontsize',16,'BackgroundColor',colorList{n},...
        'Units','normalized','Position',[0.2,1-n/3,0.6,1/3],...
        'Callback',@ChangeColor1);
end

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

BtGroup3 = cell(3,1);
for n = 1:3
    BtGroup3{n} = uicontrol(Pnl4,'style','radiobutton',...
        'String',checkBoxList{n},'Fontsize',16,'BackgroundColor',colorList{n},...
        '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(BtGroup1{i},'Value');
        end
        imnew = im.*rgb;
        imshow(imnew,'Parent',Axes1);
    end

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

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

end

四、下拉菜单

《又到毕业季》MATALB GUI 基础控件与交互_第4张图片

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)
    menuList{s} = strcat(num2str(sizeList{s}(1)),'*',num2str(sizeList{s}(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(Axes{n}) 
        end
        Axes = [];
        
        row = sizeList{tmp}(1);
        col = sizeList{tmp}(2);
        for r = 1:row
            for c = 1:col
                idx = (r-1)*col+c;
                Axes{idx} = 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',Axes{n})
       end
    end

end

五、列表

《又到毕业季》MATALB GUI 基础控件与交互_第5张图片

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','normalized','Position',[0,0.0,1,0.9],...
    'Callback',@Doit);

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

fileList = {};
fileNum = 0;
dataSet = {};

    function LoadData(~,~)
        folderPath = uigetdir('.\','请选择一个路径');
        fileList = dir(fullfile(folderPath,'*.jpg'));
        fileList = {fileList.name};
        fileNum = length(fileList);
        set(Lb,'String',fileList);        
        for n = 1:fileNum
            dataSet{n} = imread(fullfile(folderPath,fileList{n}));            
        end
        imshow(dataSet{1},'Parent',Axes)
    end

    function Doit(~,~)
        idx = get(Lb,'value');
        imshow(dataSet{idx},'Parent',Axes)
    end

end

六、滚动条

《又到毕业季》MATALB GUI 基础控件与交互_第6张图片

function GUI6()

%% 搭框架
Fig = figure('Position',[800,100,800,900],'menu','none');
Fig.Color = 'white';
Fig.NumberTitle = 'off';
Fig.Name = 'GUI6';
% 面板
Pnl1 = uipanel(Fig,'Position',[0.05,0.2,0.9,0.75]);
Pnl2 = uipanel(Fig,'Position',[0.05,0.05,0.9,0.1]);
% 绘图窗
Axes = axes(Pnl1,'Position',[0.1,0.1,0.9,0.9]);
Lbx = uicontrol(Pnl1,'style','slider',...
    'String',[],'Fontsize',12,...
    'Unit','normalized','Position',[0.1,0,0.9,0.05],...
    'Max',1,'Min',-1,...
    'Callback',@Move);
Lby = uicontrol(Pnl1,'style','slider',...
    'String',[],'Fontsize',12,...
    'Unit','normalized','Position',[0,0.1,0.05,0.9],...
    'Max',1,'Min',-1,...
    'Callback',@Move);  

% 操作窗
uicontrol(Pnl2,'style','pushbutton',...
    'String','红方用力','Fontsize',16,'BackgroundColor',[1,0.5,0.5],...
    'Unit','normalized','Position',[0,0,0.2,1],...
    'Callback',@Pull1);
uicontrol(Pnl2,'style','pushbutton',...
    'String','蓝方用力','Fontsize',16,'BackgroundColor',[0.5,0.5,1],...
    'Unit','normalized','Position',[0.8,0,0.2,1],...
    'Callback',@Pull2);
Lb = uicontrol(Pnl2,'style','slider',...
    'String',[],'Fontsize',12,...
    'Unit','normalized','Position',[0.2,0.,0.6,1],...
    'Max',1,'Min',-1); 


%% 初始化
%  初始化目标
plot(-1,0,'ro','MarkerSize',30,'Parent',Axes),hold on
plot(1,0,'bo','MarkerSize',30,'Parent',Axes)
plot([-1,1],[0,0],'k-')
obj = plot(0,0,'k.','MarkerSize',30,'Parent',Axes);
% 初始化坐标轴
range = [-1.2,1.2,-1,1];
axis(Axes,range)
grid on

%% 开始操作吧
while true
    dis = randi([-3,3])/50;
    obj.XData = min(max(obj.XData+dis,-1),1);
    set(Lb,'Value',obj.XData)            
    % 你慢一点
    pause(0.1)
    drawnow
    % 你们别打啦
    if abs(obj.XData)==1
        break
    end
    
end

%% 子函数
    function Move(~,~)
        xids = get(Lbx,'Value');
        yids = get(Lby,'Value');
        change = [xids,xids,yids,yids];
        axis(Axes,range+change)        
    end

    function Pull1(~,~)
        obj.XData = max(obj.XData-0.2,-1);
        set(Lb,'Value',obj.XData);
    end

    function Pull2(~,~)
        obj.XData = min(obj.XData+0.2,1);
        set(Lb,'Value',obj.XData);
    end

end

七、文本与编辑

《又到毕业季》MATALB GUI 基础控件与交互_第7张图片

function GUI7()

% 主界面
Fig = figure('Position',[500,200,900,600],'menu','none');
Fig.Color = 'white';
Fig.NumberTitle = 'off';
Fig.Name = 'GUI7';

% 历史记录
Pnl1 = uipanel(Fig,'Position',[0.05,0.5,0.9,0.5],...
    'Title','历史记录','Fontsize',14,'BackgroundColor','w');
% 骚操作
Pnl2 = uipanel(Fig,'Position',[0.05,0.05,0.9,0.4],...
    'Title','人工输入','Fontsize',14,'BackgroundColor','w');

% 文本框
str1 = {'欢迎使用'};
Text = uicontrol(Pnl1,'style','text',...
             'String',str1,'Fontsize',16,...
             'Units','normalized','Position',[0.05,0.05,0.9,0.9]);
% 编辑框
str2 = '请输入你想要的的内容';
Edit = uicontrol(Pnl2,'style','edit',...
    'String',str2,'Fontsize',16,...
    'Units','normalized','Position',[0.05,0.05,0.6,0.9]);

% 按钮
Bt = cell(3,1);
BtNameList = {'增加','替换','清除'};
for n = 1:3
Bt{n} = uicontrol(Pnl2,'style','pushbutton',...
            'String',BtNameList{n},'Fontsize',16,...
            'Units','normalized','Position',[0.7,0.95-n*0.3,0.25,0.3]);
end

Bt{1}.Callback = @doit1;
Bt{2}.Callback = @doit2;
Bt{3}.Callback = @doit3;

    function doit1(~,~)
        tmp = get(Edit,'String');
        str1 = cat(1,str1,{tmp});
        set(Text,'String',str1);
        set(Edit,'String','');
    end

    function doit2(~,~)
        tmp = get(Edit,'String');
        str1 = {tmp};
        set(Text,'String',str1);
        set(Edit,'String','');
    end

    function doit3(~,~)
        str1 = {};
        set(Text,'String',str1);
        set(Edit,'String','');
    end


end

八、接口与调用

《又到毕业季》MATALB GUI 基础控件与交互_第8张图片

function GUI8()

% 框架
Fig1 = figure('Position',[400,600,300,400],'menu','none');
Fig1.Color = 'white';
Fig1.NumberTitle = 'off';
Fig1.Name = 'GUI8';
% 按钮
num = 8;
for n = 1:num
        p = [0.05,1-n/num,0.9,1/num];
        Bt{n} = uicontrol(Fig1,'style','pushbutton',...
                     'String',['运行GUI',num2str(n)],'Fontsize',16,...
                     'Units','normalized','Position',p);
end
Bt{1}.Callback = @Doit1;
Bt{2}.Callback = @Doit2;
Bt{3}.Callback = @Doit3;
Bt{4}.Callback = @Doit4;
Bt{5}.Callback = @Doit5;
Bt{6}.Callback = @Doit6;
Bt{7}.Callback = @Doit7;
Bt{8}.Callback = @Doit8;

    function Doit1(~,~)
        GUI1();
    end
    function Doit2(~,~)
        GUI2();
    end
    function Doit3(~,~)
        GUI3();
    end
    function Doit4(~,~)
        GUI4();
    end
    function Doit5(~,~)
        GUI5();
    end
    function Doit6(~,~)
        GUI6();
    end
    function Doit7(~,~)
        GUI7();
    end
    function Doit8(~,~)
        close all
        GUI8();
    end

end


其他

此文为《又到毕业季》MATLAB GUI系列视频的代码合集。

你可能感兴趣的:(MATLAB,matlab,GUI,毕业设计)