一、背景介绍
采用Matlab中的App Designer开发可视化GUI分析界面后,如果将分析结果以报告的形式自动导出,将会大大提高效率。本案例将介绍如何一键自动生成报告。
二、基于 Matlab中的App Designer进行报告一键自动导出的方法介绍
本案例以数据分析及2D绘图的案例为基础,介绍报告自动导出的代码实现。
首先在可视化GUI分析界面上添加报告导出按钮。如图1所示。
图1 报告自动导出按钮添加图
2、对添加的报告导出按钮添加回调,如图2所示。
图2 对报告导出按钮添加回调
3、转到代码视图对报告导出进行代码开发。
图3 转入代码视图进行报告导出的代码开发
4、在代码视图进行代码开发,如图4所示。
filespec=['数据分析报告',datestr(now,31),'.doc'];
%对报告的名称、格式及报告时间格式进行定义
try
Word=actxGetRunningServer('word.Application');
catch
Word=actxServer('word.Application');
end
%启用word调用功能
set(Word,'Visible',1);
%使word可见
documents=Word.Documents;
if exist(filespec,'file')
document=invoke(documents,'Open',filespec);
else
document=invoke(documents,'Add');
end
%打开word文档,如果路径下没有,则创建一个空白文档打开
content=document.Content;
duplicate=content.Duplicate;
inlineshapes=content.Inlineshapes;
selection=Word.Selection;
paragraphformat=selection.Paragraphformat;
%格式定义
document.PageSetup.TopMargin=60;
document.PageSetup.BottomMargin=45;
document.PageSetup.LeftMargin=40;
document.PageSetup.RightMargin=40;
%设置页边距
%设置标题
set(content,'start',0);%起始点 为0,表示每次写入覆盖之前的内容
title='数据分析报告';%标题名称
set(content,'text',title);
set(paragraphformat,'Alignment','wdAlignParagraphCenter');%居中设置
rr=document.Range(0.16);%选择文本
rr.Font.Size=20;%设置字体大小
rr.Font.Bold=1;%设置字体加粗
end_of_doc=get(content,'end');
set(selection,'Start',end_of_doc);%开始的地方在上一个结尾
selection.TypeParagraph;%插入一个新的空段落
copygraphics(app.UIAxes,'ContentType','vector');
selection.Paste%插入输入数据曲线
end_of_doc=get(content,'end');
set(selection,'Start',end_of_doc);%开始的地方在上一个结尾
selection.Text=' ';
end_of_doc=get(content,'end');
set(selection,'Start',end_of_doc);%开始的地方在上一个结尾
copygraphics(app.UIAxes_2,'ContentType','vector');
selection.Paste%插入输出数据曲线
图4 报告自动导出代码开发图
5、完整代码如下所示
classdef Draw_2D_app_1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Button matlab.ui.control.Button
EditFieldLabel matlab.ui.control.Label
EditField matlab.ui.control.EditField
Button_2 matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
Label matlab.ui.control.Label
Label_2 matlab.ui.control.Label
UIAxes_2 matlab.ui.control.UIAxes
Label_3 matlab.ui.control.Label
Button_3 matlab.ui.control.Button
end
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
clc
[filename,pathname]=uigetfile({'*.xls';'*.xlsx';'*.*'},'File selector');
pathname_EditField=string(pathname);
filename_EditField=string(filename);
if ~isequal(filename,0)
app.EditField.Value=fullfile(pathname,filename);
else
return
end
filename_Ed=app.EditField.Value;
%Excel中的数据导入,并转化成数组
pat_1=readtable(filename_Ed,'Sheet',1);
global P_1
global S_1
P_1=table2array(pat_1(:,1));
S_1=table2array(pat_1(:,2));
plot(app.UIAxes,P_1,S_1)%在坐标区内进行绘图
end
% Button pushed function: Button_2
function Button_2Pushed(app, event)
global P_1
global S_1
K_1=23;
P_2=K_1*P_1;
F_2=K_1*S_1+5;
plot(app.UIAxes_2,P_2,F_2)%在坐标区内进行绘图
end
% Button pushed function: Button_3
function Button_3Pushed(app, event)
filespec=['数据分析报告',datestr(now,31),'.doc'];
%对报告的名称、格式及报告时间格式进行定义
try
Word=actxGetRunningServer('word.Application');
catch
Word=actxServer('word.Application');
end
%启用word调用功能
set(Word,'Visible',1);
%使word可见
documents=Word.Documents;
if exist(filespec,'file')
document=invoke(documents,'Open',filespec);
else
document=invoke(documents,'Add');
end
%打开word文档,如果路径下没有,则创建一个空白文档打开
content=document.Content;
duplicate=content.Duplicate;
inlineshapes=content.Inlineshapes;
selection=Word.Selection;
paragraphformat=selection.Paragraphformat;
%格式定义
document.PageSetup.TopMargin=60;
document.PageSetup.BottomMargin=45;
document.PageSetup.LeftMargin=40;
document.PageSetup.RightMargin=40;
%设置页边距
%设置标题
set(content,'start',0);%起始点 为0,表示每次写入覆盖之前的内容
title='数据分析报告';%标题名称
set(content,'text',title);
set(paragraphformat,'Alignment','wdAlignParagraphCenter');%居中设置
rr=document.Range(0.16);%选择文本
rr.Font.Size=20;%设置字体大小
rr.Font.Bold=1;%设置字体加粗
end_of_doc=get(content,'end');
set(selection,'Start',end_of_doc);%开始的地方在上一个结尾
selection.TypeParagraph;%插入一个新的空段落
copygraphics(app.UIAxes,'ContentType','vector');
selection.Paste%插入输入数据曲线
end_of_doc=get(content,'end');
set(selection,'Start',end_of_doc);%开始的地方在上一个结尾
selection.Text=' ';
end_of_doc=get(content,'end');
set(selection,'Start',end_of_doc);%开始的地方在上一个结尾
copygraphics(app.UIAxes_2,'ContentType','vector');
selection.Paste%插入输出数据曲线
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Color = [0.5765 0.9804 0.9412];
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Icon = 'jiazai_1.JPG';
app.Button.BackgroundColor = [0.0588 1 1];
app.Button.Position = [429 379 51 51];
app.Button.Text = '';
% Create EditFieldLabel
app.EditFieldLabel = uilabel(app.UIFigure);
app.EditFieldLabel.HorizontalAlignment = 'right';
app.EditFieldLabel.FontWeight = 'bold';
app.EditFieldLabel.Position = [40 393 53 22];
app.EditFieldLabel.Text = '参数输入';
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'text');
app.EditField.FontWeight = 'bold';
app.EditField.Position = [108 393 282 22];
% Create Button_2
app.Button_2 = uibutton(app.UIFigure, 'push');
app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true);
app.Button_2.Icon = '234.JPG';
app.Button_2.BackgroundColor = [0 1 1];
app.Button_2.FontSize = 13;
app.Button_2.FontWeight = 'bold';
app.Button_2.Position = [132 44 85 83];
app.Button_2.Text = '';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, '压力与变形量关系曲线')
xlabel(app.UIAxes, '压力/bar')
ylabel(app.UIAxes, '变形量/mm')
app.UIAxes.GridColor = [0.149 0.149 0.149];
app.UIAxes.BoxStyle = 'full';
app.UIAxes.TitleFontWeight = 'bold';
app.UIAxes.BackgroundColor = [0.5843 0.9882 0.9686];
app.UIAxes.Position = [12 149 300 185];
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.FontName = 'Times New Roman';
app.Label.FontSize = 15;
app.Label.FontWeight = 'bold';
app.Label.Position = [59 350 95 22];
app.Label.Text = '输入数据曲线';
% Create Label_2
app.Label_2 = uilabel(app.UIFigure);
app.Label_2.FontName = 'Times New Roman';
app.Label_2.FontSize = 15;
app.Label_2.FontWeight = 'bold';
app.Label_2.Position = [375 350 95 22];
app.Label_2.Text = '分析数据曲线';
% Create UIAxes_2
app.UIAxes_2 = uiaxes(app.UIFigure);
title(app.UIAxes_2, '压力与变形量关系曲线')
xlabel(app.UIAxes_2, '压力/bar')
ylabel(app.UIAxes_2, '变形量/mm')
app.UIAxes_2.BoxStyle = 'full';
app.UIAxes_2.TitleFontWeight = 'bold';
app.UIAxes_2.BackgroundColor = [0.549 0.9765 1];
app.UIAxes_2.Position = [328 149 300 185];
% Create Label_3
app.Label_3 = uilabel(app.UIFigure);
app.Label_3.FontName = 'Times New Roman';
app.Label_3.FontSize = 15;
app.Label_3.FontWeight = 'bold';
app.Label_3.Position = [261 443 119 22];
app.Label_3.Text = '数据分析及绘图';
% Create Button_3
app.Button_3 = uibutton(app.UIFigure, 'push');
app.Button_3.ButtonPushedFcn = createCallbackFcn(app, @Button_3Pushed, true);
app.Button_3.Icon = '234s.JPG';
app.Button_3.BackgroundColor = [0 1 1];
app.Button_3.FontSize = 13;
app.Button_3.FontWeight = 'bold';
app.Button_3.Position = [435 44 85 75];
app.Button_3.Text = '';
end
end
methods (Access = public)
% Construct app
function app = Draw_2D_app_1
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
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
三、总结
本文采用案例讲解的方式介绍了采用Matlab中的App Designer开发可视化GUI分析界面后,实现报告一键自动导出的方法和步骤。可以实现从报告编制等繁琐的工作中解脱出来。