matlab自动生成word【转】
2013-04-08 05:37阅读:
一些工作是程式化的,做了一个就有了基本框架,其它的只要按照框架修改一些数据就行了,为了方便,我们有必要自动生成一个word文本,这样既减少输入错误,还为你节省了宝贵的时间,下面是我在工作中需要处理一些数据异常时用的程序(函数MyHT_AutoWord),效果不错,放上来,让同志们指正或借鉴!
f
unction MyHT_AutoWord
%利用MATLAB生成Word文档
%
MyHT_AutoWord
% Copyright
2011 suhejun.
% $Revision:
1.0.0.0 $ $Date: 2011/01/28 17:27:00 $
%%
一、首先是数据处理并成浓度曲线图
clear,clc
[fname,pname]=uigetfile('*.txt','open the date
file');
fullname=strcat(pname,fname);
fidin=fopen(fullname,'rt');
if(fidin==-1)
display('no file vas
selected');
return
end
[a,b1,c1]=textread(fullname,'%f%f%f'); %
打开一般的没有分割符的文件.
b=0.01143*b1;
c=0.1693*c1;
%
第一步:剔除单点异常
%
-------------------------------------------------------------------------
n=length(b);
for i=1:n-1
if i==1;
if b(i)>2*b(i+1) & b(i)>2*b(i+2);
b(i)=(b(i+1)+b(i+2))/2;
end
if c(i)>2*c(i+1) & c(i)>2*c(i+2);
c(i)=(c(i+1)+c(i+2))/2;
end
else
if b(i)>2*b(i+1) & b(i)>2*b(i-1);
b(i)=(b(i+1)+b(i-1))/2;
end
if c(i)>2*c(i+1) & c(i)>2*c(i-1);
c(i)=(c(i+1)+c(i-1))/2;
end
end
end
%
第二步:判断异常下限
%
-------------------------------------------------------------------------
mean_hg=mean(b);
mean_rn=mean(c);
std_hg=std(b);
std_rn=std(c);
%
%没有除掉异常值计算的平均值
%
normal_hg=mean_hg+2.*std_hg;
%
normal_rn=mean_rn+2.*std_rn;
%除掉异常值计算的平均值(把大于整体平均值的不计于背景值)
normal_hg=mean(b(b<=mean_hg))+2.*std_hg;
normal_rn=mean(c(c<=mean_rn))+2.*std_rn;
texthgy1=normal_hg;
texthgy2=mean_hg;
textrny1=normal_rn;
textrny2=mean_rn;
%
第三步:绘制曲线图
%
-------------------------------------------------------------------------
zft = figure('units','normalized','position',...
[0.280469 0.553385
0.428906 0.251302],'visible','off');
subplot(2,1,1);
xlim([0 max(a)]);
y1=b;
%
测量数据
x=a;
%
注意:自变量要单调变化
y2=texthgy1.*[ones(1,length(x))];
area(x',y1',texthgy1,'FaceColor','g');hold
on
area(x',y2',min(y1),'FaceColor','w','EdgeColor','w');hold
on
max_y1=max(y1);
beishu1=max_y1./texthgy2;
plot(a,b,'g-',a,b,'o','MarkerEdgeColor','g','MarkerFaceColor','white','MarkerSize',6);
xlabel('点距(m)');ylabel('Hg(ng/l)');
hold on
t=0:max(xlim);
plot(t,normal_hg);hold
on
plot(t,mean_hg);
hold on
text(a(length(a)-10),texthgy1,strcat('n=','(',num2str(beishu1),')'));
text(a(length(a)-5),texthgy1,strcat('最大值','(',num2str(max_y1),')'));
text(a(length(a)-8),texthgy1,strcat('异常下限','(',num2str(texthgy1),')'));
text(a(length(a)-8),texthgy2,strcat('背景值','(',num2str(texthgy2),')'));
box on;
str=fname;
ind=findstr(str,'.');
str2=str(1:ind-1);
text(a(2),max(y1),str2);
%%-----------------------
subplot(2,1,2);
xlim([0 max(a)]);
y1=c;
%
测量数据
x=a;
%
注意:自变量要单调变化
y2=textrny1.*[ones(1,length(x))];
area(x',y1',textrny1,'FaceColor','r');hold
on
area(x',y2',min(y1(y1>0)),'FaceColor','w','EdgeColor','w');hold
on
max_y2=max(y1);
beishu2=max_y2./textrny2;
plot(a,c,'r-',a,c,'rsq','MarkerEdgeColor','r','MarkerFaceColor','white','MarkerSize',5)
xlabel('点距(m)');ylabel('Rn(Bq/l)');
hold on
t=0:max(xlim);
hline3=plot(t,normal_rn,'k');
hline4=plot(t,mean_rn,'k');
hold on
text(a(length(a)-10),textrny1,strcat('n=','(',num2str(beishu2),')'));
text(a(length(a)-5),textrny1,strcat('最大值','(',num2str(max_y2),')'));
text(a(length(a)-8),textrny1,strcat('异常下限','(',num2str(textrny1),')'));
text(a(length(a)-8),textrny2,strcat('背景值','(',num2str(textrny2),')'));
box on;
text(a(2),max(y1),str2);
hold off
%将图形复制到粘贴板
hgexport(zft,
'-clipboard');
%%
二、成word文本并插入前面的图
%
设定测试Word文件名和路径
filespec_user = [pwd
'\化探分析结果.doc'];
%
判断Word是否已经打开,若已打开,就在打开的Word中进行操作,否则就打开Word
try
%
若Word服务器已经打开,返回其句柄Word
Word =
actxGetRunningServer('Word.Application');
catch
%
创建一个Microsoft
Word服务器,返回句柄Word
Word =
actxserver('Word.Application');
end;
%
设置Word属性为可见
Word.Visible = 1;
%
或set(Word,
'Visible', 1);
%
若测试文件存在,打开该测试文件,否则,新建一个文件,并保存,文件名为测试.doc
ifexist(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.SaveAs(filespec_user);
end
Content = Document.Content;
% 返回Content接口句柄
Selection = Word.Selection;
% 返回Selection接口句柄
Paragraphformat =
Selection.ParagraphFormat; % 返回ParagraphFormat接口句柄
%
页面设置
Document.PageSetup.TopMargin =
60; % 上边距60磅
Document.PageSetup.BottomMargin
= 45; % 下边距45磅
Document.PageSetup.LeftMargin =
45; % 左边距45磅
Document.PageSetup.RightMargin
= 45; % 右边距45磅
%
设定文档内容的起始位置和标题
Content.Start = 0;
%
设置文档内容的起始位置
title = str2;
Content.Text = title;
% 输入文字内容
Content.Font.Size = 16 ;
%
设置字号为16
Content.Font.Bold = 4 ;
%
字体加粗
Content.Paragraphs.Alignment
= 'wdAlignParagraphLeft'; % 居中对齐
Selection.Start = Content.end;
% 设定下面内容的起始位置
Selection.TypeParagraph;
% 回车,另起一段
xueqi =
strcat('
图**为',str2,'汞氡浓度异常曲线,','由图可知:汞浓度曲线出现一组峰值异常形态,其最大值',num2str(max_y1),'ng/L是背景值',num2str(texthgy2),'ng/L的',num2str(beishu1),'倍;','氡浓度曲线也出现*组峰值异常形态,其最大值',num2str(max_y2),'Bq/L是背景值',num2str(textrny2),'Bq/L的',num2str(beishu2),'倍。');
Selection.Text = xueqi;
% 在当前位置输入文字内容
Selection.Font.Size = 12;
%
设置字号为12
Selection.Font.Bold = 0;
% 字体不加粗
Selection.MoveDown;
%
光标下移(取消选中)
paragraphformat.Alignment
= 'wdAlignParagraphCenter'; % 居中对齐
Selection.TypeParagraph;
% 回车,另起一段
Selection.TypeParagraph;
% 回车,另起一段
Selection.Font.Size =
10.5; %
设置字号为10.5
Selection.Start = Content.end;
% 设定下面内容的起始位置
Selection.PasteSpecial;
delete(zft);
%
删除图形句柄
Document.ActiveWindow.ActivePane.View.Type =
'wdPrintView';
%
设置视图方式为页面
Document.Save;
%
保存文档
来自:http://hi.baidu.com/suhejunsuhejun/item/21cb903caed8366f7c034b56