经过前面两个实例的练习,接下来正式开始为系统的设计作铺垫。首先学习的是用MATLAB GUI怎么插入表格并进行编辑。
uitable的创建与设置
第一步:创建一个空的GUI界面,在界面上添加一个uitable控件。
第二步:设置 uitable 的属性和显示效果。设置 uitable 属性的方法有两种:采用 Table Property Editor 设置 uitable 的属性或者采用 MATLAB 代码设置 uitable 的属性。
采用 Table Property Editor 设置 uitable 的属性:
- 设置所创建的uitable 的Tag为Tag1;
- 设置列标题ColumnName、列宽、列设置为可编辑状态,设置行标题;
- 初始化uitable的数据,WS里生成magic(5)矩阵,用于数据初始化;
- 设置uitable的背景颜色(BackgroundColor)的分段效果。
采用 MATLAB 代码设置 uitable 的属性:
- 设置所创建的uitable 的Tag为Tag2;
- 设置列标题ColumnName、列宽、列设置为可编辑状态;
set(hObject,'ColumnName',{'列号1','列号2','列号3','列号4','列号5'}) % 设置列标题
set(hObject,'ColumnWidth',{100,100,100,100,100}) % 设置列宽
set(hObject,'ColumnEditable',logical(ones(1,5))) % 设置列的可编辑性
- 设置行标题;
set(hObject,'RowName',{'A','B','C','D','E'}) % 设置行标题
- 初始化uitable的数据
set(hObject,'Data',magic(5)) % 初始化数据
- 设置uitable的背景颜色(BackgroundColor)的分段效果。
set(hObject,'BackgroundColor',[0 1 1;1 1 0]) % 设置行的分段效果
实例一: 某工厂生产的桶装洗衣液,产品重量要求为3±0.05Kg。现质检部门对一批产品进行抽样检查,由3个不同的测量员分别对5桶洗衣液进行称重,称重结果见“chengzhong.xlsx”文件。将称重结果显示在一个uitable中,并绘制称重结果的图形。
第一步:GUIDE画界面。
第二步:编辑代码。
function varargout = chengzhong(varargin)
% CHENGZHONG MATLAB code for chengzhong.fig
% CHENGZHONG, by itself, creates a new CHENGZHONG or raises the existing
% singleton*.
%
% H = CHENGZHONG returns the handle to a new CHENGZHONG or the handle to
% the existing singleton*.
%
% CHENGZHONG('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CHENGZHONG.M with the given input arguments.
%
% CHENGZHONG('Property','Value',...) creates a new CHENGZHONG or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before chengzhong_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to chengzhong_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 chengzhong
% Last Modified by GUIDE v2.5 10-Apr-2019 21:36:39
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @chengzhong_OpeningFcn, ...
'gui_OutputFcn', @chengzhong_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 chengzhong is made visible.
function chengzhong_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 chengzhong (see VARARGIN)
% Choose default command line output for chengzhong
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes chengzhong wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = chengzhong_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 pushbutton1.
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)
[filename,pathname]=uigetfile({'*.xlsx';'*.xls'},'读入测试数据文件');
[num,txt,raw]=xlsread([pathname,filename])
set(handles.Tab1,'Data',num(:,2:end)) %显示到表格中
% --- Executes during object creation, after setting all properties.
function Tab2_CreateFcn(hObject, eventdata, handles)
% hObject handle to Tab2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
set(hObject,'ColumnName',{'重量下限值','绘图间隔值','重量上限值'}) % 设置列标题
set(hObject,'Data',[2.95,0.02,3.05])
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
TestData=get(handles.Tab1,'Data') %获取表格数据矩阵
Limits=get(handles.Tab2,'Data')
axes(handles.axes1)
plot(1:5,TestData,'Marker','o')
hold on
plot([1,5],[Limits(1),Limits(1)],'r--','LineWidth',2)
plot([1,5],[Limits(3),Limits(3)],'r--','LineWidth',2)
set(gca,'YTick',[Limits(1):Limits(2):Limits(3)]) %标上小刻度
set(gca,'YLim',[Limits(1)-0.01 Limits(3)+0.01]) %设置Y坐标范围
legend({'测量员甲','测量员乙','测量员丙','重量下限','重量上'},'Location','NorthEastOutside') %设置图例,以及位置
hold off %方便下次改变数值的时候,刷新界面
% --- Executes during object creation, after setting all properties.
function Tab1_CreateFcn(hObject, eventdata, handles)
% hObject handle to Tab1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
set(hObject,'ColumnName',{'测量员甲','测量员乙','测量员丙'}) % 设置列标题
% --- Executes on button press in save.
function save_Callback(hObject, eventdata, handles)
% hObject handle to save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,pathname] = uiputfile({'*.xls','excel(*.xls)'}, '保存数据','Untitled');
if filename==0
return
end
Data=get(handles.Tab1,'Data');
xlswrite(fullfile(pathname,filename),Data);
% --- Executes on button press in exit.
function exit_Callback(hObject, eventdata, handles)
% hObject handle to exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc %清除指令窗
close all %关闭所有句柄可见的窗口
close(gcf) %关闭当前窗口
clear %清除内存变量和函数
第三步:运行。
第四步:在完成GUI界面创建后,对所创建界面的效果进行试验。点击“导入测量数据”按钮在文件夹中选择“chengzhong.xlsx”文件,点击“保存测量数据”按钮可将测量的数据另存为新的文件,点击“绘制测量数据”按钮即可绘制称重结果的图形,点击“退出”按钮可以退出界面。