Matlab批量画图并写入word
参考:
基本语法
- 调用word
try
Word = actxGetRunningServer('Word.Application');
catch
Word = actxserver('Word.Application');
end
Word.Visible = 1; % 使word为可见;或set(Word, 'Visible', 1);
- 打开word
if exist(filespec_user,'file');
Document = Word.Documents.Open(filespec_user);
else
Document = Word.Documents.Add;
Document.SaveAs2(filespec_user);
end
- 格式定义
Content = Document.Content;
Selection = Word.Selection;
Paragraphformat = Selection.ParagraphFormat;
- 页边距
Document.PageSetup.TopMargin = 60;
Document.PageSetup.BottomMargin = 45;
Document.PageSetup.LeftMargin = 45;
Document.PageSetup.RightMargin = 45;
- 文本写入
Content.Start = 0; % 起始点为0,即表示每次写入覆盖之前资料
Content.Text = headline;
Content.Font.Size = 20; % 字体大小
Content.Font.Bold = 1; % 字体加粗
Content.Paragraphs.Alignment = 'wdAlignParagraphCenter';
% 居左 wdAlignParagraphLeft
% 居中 wdAlignParagraphCenter
% 居右 wdAlignParagraphRight
- 插入回车
Selection.TypeParagraph; % 插入一个新的空段落
Selection.Font.Size = 10.5; % 新的空段落字号
- 绘图
f1=figure; % 记录其句柄
x=0:0.02:10;
y=cos(x);
plot(x,y,'r')
ylabel('cos');title('cos(rad)');
hgexport(f1, '-clipboard'); %figure的句柄为“f1”
Selection=Word.Selection;
Selection.Range.Paste; %在当前光标的位置插入图片
Selection.Start = Content.end;
Selection.Text = '图1 cosx的曲线';
Paragraphformat.Alignment = 'wdAlignParagraphCenter'; %居中显示
Selection.MoveDown;
Selection.TypeParagraph;% 插入一个新的空段落
批处理对数据进行重复画图
在处理重复性实验数据的时候,需要对大量的数据进行画图分析,如何简单高效的实现画图呢
一个解决办法就是把txt中的数据在matlab中绘制出来,并按照其本身的名字保存为word文件,这样有利于后期对数据直观的进行分析.
需要的实现过程
- 数据的载入
[TxtFileName,TxtPath,~] = uigetfile({'*.mat'},'MultiSelect','On');
该语句的功能为打开一个弹窗,根据选择的文件,保存其名字和路径.
{'*.mat'}
是弹窗中可以看到的文件格式,可以增加;
'MultiSelect'
功能是多选开关,on
为可以多选,off
为单选;
- 读取文件
if iscell(TxtFileName(:,2)) == 1 % 判断TxtFileName是char还是元胞
Len = size(TxtFileName,2);
% 如果uigetfile中只选择了一个,那么会使TxtFileName{index}不是一个元胞而报错,
% 解决办法是,增加一步判断语句,如果是数据类型是char,说明只选择了一个文件
% 如果是元胞,说明选择了多个文件.
else
Len = 1;
end
for index = 1:Len
if Len == 1
FileName = TxtFileName;
else
FileName = TxtFileName{index};
end
path = TxtPath;
FileName = strcat(path,FileName);
clearvars -except index FileName TxtFileName TxtPath Len % 清除其他数据
load(FileName); % 载入数据
% ... 主程序
end
- word初始化和文件标题
filespec_user = [pwd '\飞行数据分析_',num2str(FileName(end-11:end-4)),'.doc'];% 设定测试Word文件名和路径
% 判断Word是否已经打开,若已打开,就在打开的Word中进行操作,否则就打开Word
try
% 若Word服务器已经打开,返回其句柄Word
Word = actxGetRunningServer('Word.Application');
catch
% 否则,创建一个Microsoft Word服务器,返回句柄Word
Word = actxserver('Word.Application');
end
Word.Visible = 1; % 或set(Word, 'Visible', 1);
% 若测试文件存在,打开该测试文件,否则,新建一个文件,并保存,文件名为测试.doc
if exist(filespec_user,'file')
Document = Word.Documents.Open(filespec_user);
% Document = invoke(Word.Documents,'Open',filespec_user);
else
Document = Word.Documents.Add;
% Document = invoke(Word.Documents, 'Add');
Document.SaveAs2(filespec_user);
end
%===格式定义==============================================================
Content = Document.Content;
Selection = Word.Selection;
Paragraphformat = Selection.ParagraphFormat;
% 文档的标题及格式
headline = '无人机飞行报告';
Content.Start = 0; % 起始点为0,即表示每次写入覆盖之前资料
Content.Text = headline;
Content.Font.Size = 20; % 字体大小
Content.Font.Bold = 1; % 字体加粗
Content.Paragraphs.Alignment = 'wdAlignParagraphCenter'; % 居中,wdAlignParagraphLeft/Center/Right
% 文档的创建时间
Selection.Start = Content.end; % 开始的地方在上一个的结尾
Selection.TypeParagraph; % 插入一个新的空段落
Selection.Start = Content.end; % 开始的地方在上一个的结尾
Selection.TypeParagraph; % 插入一个新的空段落
% 插入时间
currentdate = datestr(now, 0); % 获取当前时间
Selection.Text = currentdate; % 当前时间作为输出
Selection.Font.Size = 12; % 字号
Selection.Font.Bold = 0; % 不加粗
Selection.MoveDown; %将所选内容向下移动,并返回移动距离的单位数
Paragraphformat.Alignment = 'wdAlignParagraphCenter'; % 居中
% 插入回车
Selection.TypeParagraph;% 插入一个新的空段落
Selection.Font.Size = 10.5;% 新的空段落字号
% 插入无人机车次序号
Selection.Start = Content.end; % 开始的地方在上一个的结尾
Selection.Text = ['飞行车次为:',num2str(FileName(end-11:end-4))]; % 当前时间作为输出
Selection.Font.Size = 12; % 字号
Selection.Font.Bold = 0; % 不加粗
Selection.MoveDown; %将所选内容向下移动,并返回移动距离的单位数
Paragraphformat.Alignment = 'wdAlignParagraphCenter'; % 居中
% 插入回车
Selection.TypeParagraph;% 插入一个新的空段落
Selection.MoveDown; %将所选内容向下移动,并返回移动距离的单位数
Selection.TypeParagraph;% 插入一个新的空段落
Selection.MoveDown; %将所选内容向下移动,并返回移动距离的单位数
- 画图并保存至word
f1 = figure;
plot3(Flight_data.Center(1:end,1),...
Flight_data.Center(1:end,2),Flight_data.Center(1:end,3),'k.');
hold on;
plot3(Flight_data.Center(1,1),Flight_data.Center(1,2),Flight_data.Center(1,3),'ro'); % 起点
xlabel('X');ylabel('Y');zlabel('Z');
set(gca,'ZDir','reverse');
title('UAV 3D trajectory');
f2=figure;
subplot(3,1,1)
plot(Flight_data.Center(1:end,1),'r')
ylabel('X');title('XYZ');
subplot(3,1,2)
plot(Flight_data.Center(1:end,2),'b')
ylabel('Y');
subplot(3,1,3)
plot(-Flight_data.Center(1:end,3),'k')
ylabel('-Z');
hgexport(f1, '-clipboard');%figure的句柄为“zft”
Selection=Word.Selection;
Selection.Range.Paste;%在当前光标的位置插入图片
Selection.Start = Content.end;
Selection.Text = '图1 3D轨迹';
Paragraphformat.Alignment = 'wdAlignParagraphCenter';
Selection.MoveDown;
Selection.TypeParagraph;% 插入一个新的空段落
hgexport(f2, '-clipboard');%figure的句柄为“zft”
Selection=Word.Selection;
Selection.Range.Paste;%在当前光标的位置插入图片
Selection.Start = Content.end;
Selection.Text = '图2 XYZ';
Paragraphformat.Alignment = 'wdAlignParagraphCenter';
Selection.MoveDown;
Selection.TypeParagraph;% 插入一个新的空段落
- 保存退出word
Document.ActiveWindow.ActivePane.View.Type = 'wdPrintView'; % 设置视图方式为页面
Document.Save; % 保存文档
Document.Close; % 关闭文档
Word.Quit; % 退出word服务器
即可实现对数据的重复性处理并在word中画图.