您的位置:

Matlab GUI界面设计

一、Matlab的GUI界面设计介绍

Matlab是一种可视化+编程的高级计算软件,GUI(Graphical User Interface)即图形用户界面,它是一种基于Matlab程序语言的图形化交互界面。Matlab的GUI界面设计可以使程序更加用户友好,可以直接对程序做出操作。

Matlab的GUI界面设计参考和借鉴了许多其他软件的操作界面,所以Matlab的界面设计也非常符合人的使用习惯,使用Matlab的GUI界面设计可以很好的让人们更快更好更方便地使用Matlab。

在Matlab中,利用GUI能够实现比较友好地界面交互,简单明了的显示各种数据和结果,互动性也非常不错,尤其是数据可视化上,Matlab GUI的体验是比较完美的。

二、Matlab的GUI界面设计优点

1、提高程序的可扩展性

利用Matlab的GUI界面设计设计的程序可以很方便的增加或者修改功能,满足用户的不同需求。

2、提高程序的可读性和可维护性

Matlab的GUI界面设计可以让程序的代码更加结构化,让代码的逻辑更加清晰,代码的可读性和可维护性得到了很大的提高,减少了代码的出错率。

3、提高程序的易用性

Matlab的GUI界面设计使得程序界面更加友好,用户体验更加优秀,从而提高了程序的易用性。

三、Matlab的GUI界面设计缺点

1、当GUI程序变得复杂时,程序的结构容易变得混乱。

2、很多操作虽然可视化,但却并不直观。

3、GUI节约的是程序员的时间和精力,但却是以消耗用户时间和计算机资源为代价。

四、Matlab的GUI界面设计书籍

1、《MATLAB GUI 程序设计》(第二版)作者:(美)Stephen J.Chapman / 刘毅等

2、《Matlab图形界面设计》作者:郭天龙 李和平

3、《跟我一起学GUI入门到精通》作者:沈海龙

4、《MATLAB图形用户界面设计案例教程》作者:李正友 包良文

五、Matlab的GUI界面设计实验报告


function PushButton1_Callback(hObject, eventdata, handles)
% hObject    handle to PushButton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global rgb;
% 提示选择图像
filename = uigetfile('*.*','请选择一幅图像');
if ~filename
msgbox('请选择正确的图像格式!','错误');
return;
end
img = imread(filename);%读入图像
img_info = imfinfo(filename);%获取图像参数信息
set(handles.figure1,'UserData',img_info);%保存图像信息
axes(handles.axes1);% 激活需要插图的画图区域 
imshow(img);%显示图像
rgb = img;
%将原始图像参数信息写入到文件(先删除旧日志)
if exist('raw_data.log','file')
delete raw_data.log;
end
fid = fopen('raw_data.log','a');
fprintf(fid,'图像像素值(32位):%d × %d × %d\n',img_info.Height,img_info.Width,img_info.BitDepth);
fprintf(fid,'图像实际大小:%d字节\n',img_info.FileSize);
fprintf(fid,'图像长×宽×通道:%d×%d×%d\n',size(img,1),size(img,2),size(img,3));
fprintf(fid,'图像彩色类型:');is_rgb='';
if size(img,3)==1,is_rgb='灰度图';elseif size(img,3)==3,is_rgb='真彩色';end,fprintf(fid,'%s\n',is_rgb);
fclose(fid);

六、Matlab的GUI界面设计贪吃蛇


function snake_game

%隐藏背景的边框,增加游戏美观性
set(gcf,'Toolbar','none','Menubar','none','Color',[0.3,0.3,0.3],...
 'NumberTitle','off','Name','贪食蛇 ver1.0'); 
%初始化位置数据
SNAKE = [5 3;5 4;5 5];%初始3点蛇
APPLE = randi(20,1,2);%绘制apple
DIRECTION = [0 1];%初始运动方向设为向右
pp = 1;
while 1
    %绘制蛇和苹果
    apple = line(APPLE(1),APPLE(2),'Marker','o','Markersize',12,'Color',[1,0,0]);hold on
    snake = line(SNAKE(:,1),SNAKE(:,2),'color',[1,1,1],'linewidth',3);
    %等待用户运动
    [SNAKE,DIRECTION,APPLE] = cmd(SNAKE,DIRECTION,APPLE);
    %更新蛇的位置和判断是否产生苹果
    [SNAKE,APPLE] = move(SNAKE,DIRECTION,APPLE);
    if isempty(SNAKE) %蛇死亡
        break;
    end
    delete(snake);delete(apple);
    pp = pp+1;
end
msgbox(sprintf('你只活了%d秒。',pp),'game over!');

%贪吃蛇移动
function [SNAKE,APPLE] = move(SNAKE,DIRECTION,APPLE)
global F
%移动以前记录蛇尾,目的是决定是否吃了“苹果”
TAIL = SNAKE(end,:);
%删除最后一节蛇
SNAKE = SNAKE(1:end-1,:);
%在头部增加一节蛇
SNAKE = [SNAKE(1,:); mod(SNAKE(1,:)+DIRECTION,21)];
%判断苹果是否被吃掉
if all(SNAKE(1,:) == APPLE)
    if F=='easy'
        SNAKE(end+1,:) = TAIL;
    elseif F=='hard'
        SNAKE = [SNAKE;TAIL];
    end
    APPLE = randi(20,1,2);%重新生成苹果
end
pos = SNAKE(:,1)>=1&SNAKE(:,2)>=1&SNAKE(:,1)<=20&SNAKE(:,2)<=20;
if sum(pos)~=length(pos) || length(unique(SNAKE,'rows'))~=length(pos),
    SNAKE = [];
end

%贪吃蛇的运动控制
function [SNAKE,DIRECTON,APPLE] = cmd(SNAKE,DIRECTION,APPLE)
global F

key = get(gcf,'CurrentCharacter');%获取当前按下的键
if ~isempty(key)
    if key==28&&(DIRECTION(1)~=1&&DIRECTION(2)~=0) %左
        DIRECTION = [0 -1];
    elseif key==29&&(DIRECTION(1)~=0&&DIRECTION(2)~=1) %下
        DIRECTION = [1 0];
    elseif key==30&&(DIRECTION(1)~=0&&DIRECTION(2)~=-1) %上
        DIRECTION = [-1 0];
    elseif key==31&&(DIRECTION(1)~=-1&&DIRECTION(2)~=0) %右
        DIRECTION = [0 1];
    elseif ~isempty(strfind(lower(key),'e'))
        F = 'easy';
    elseif ~isempty(strfind(lower(key),'h'))
        F = 'hard';
    elseif key==27 %esc键
        SNAKE = [];
    end
end

%启动程序
figure('Color',[0.3,0.3,0.3]),clf,hold on;axis off,axis([1 21 1 21]);
text(16,19,'1:简单','color','y','fontsize',12);text(16,17,'2:困难','color','y','fontsize',12);
text(16,10,sprintf('按移动键或e/h进入游戏\n困难模式下增加蛇长'),'color','w','fontsize',12);
F = 'easy';SNAKE = [];while isempty(SNAKE),[r,c] = find((mod(randperm(400),20)+1)'==[1 2 3]);SNAKE = [r(1) c(1);r(2) c(2);r(3) c(3)];end;APPLE = randi(20,1,2); 
move_times = 0;

%游戏暂停函数
function [SNAKE,DIRECTON,APPLE] = pause_game(SNAKE,DIRECTION,APPLE)
while 1
    pause(0.1);
    key = get(gcf,'CurrentCharacter');
    if key == 27
        break;
    end
end

七、Matlab的GUI界面设计出题嘛


function varargout = math_tests(varargin)
% MATH_TESTS MATLAB code for math_tests.fig
%      MATH_TESTS, by itself, creates a new MATH_TESTS or raises the existing
%      singleton*.
%
%      H = MATH_TESTS returns the handle to a new MATH_TESTS or the handle to
%      the existing singleton*.
%
%      MATH_TESTS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MATH_TESTS.M with the given input arguments.
%
%      MATH_TESTS('Property','Value',...) creates a new MATH_TESTS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before math_tests_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to math_tests_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help math_tests

% Last Modified by GUIDE v2.5 22-May-2019 09:31:33

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @math_tests_OpeningFcn, ...
                   'gui_OutputFcn',  @math_tests_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before math_tests is made visible.
function math_tests_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to math_tests (see VARARGIN)

% Choose default command line output for math_tests
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes math_tests wait for user response (see UIRESUME)
% uiwait(handles.figure1);

global result
result=0;


% --- Outputs from this function are returned to the command line.
function varargout = math_tests_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in start_btn.
function start_btn_Callback(hObject, eventdata, handles)
% hObject    handle to start_btn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global result
result = 0;