找回密码
 注册
Simdroid-非首页
查看: 390|回复: 13

本人编写(借鉴了本论坛)的matlab生成word报告的代码

[复制链接]
发表于 2009-12-10 12:21:41 | 显示全部楼层 |阅读模式 来自 天津
望版主加点辛苦分!

function main_program
clc;clear;
%----注意:以下两行需要修改具体数字---------
x=119.23;y=38.71;mag=7.2;
t='2009年12月2日13时41分(北京时间)';
%-----------------------------------------
x=num2str(x);
y=num2str(y);
mag=num2str(mag);
a=load('D:\tjpg\matlab_pg\earthq_intens.txt');
%=========control the corner interp nodes==============
d1=0.8*mean(a(:,3));
d2=[116.7 38.5 d1;
    116.7 40.3 d1;
    118.1 38.5 d1;
    118.1 40.3 d1];
a=[a
    d2];
%=========earthquake intensity interp==================
num=[];lon2=[];la2=[];PGA=[];
fp2=fopen('D:\tjpg\matlab_pg\cal_nodes.txt','r');
sr=fgetl(fp2);
while 1
   sr=fgetl(fp2);
    if ~ischar(sr), break, end  
    if isempty(sr),break,end
    [token,rem]=strtok(sr,',');
    %numb=str2num(token);
    %num=[num
    %     numb];  
    [token,rem]=strtok(rem,',');
    long=str2num(token); lat=str2num(rem);
    lon2=[lon2;
        long];  
     la2=[la2;
         lat];  
end
fclose(fp2);
PGA=griddata(a(:,1),a(:,2),a(:,3),lon2,la2,'linear');
%-=====================================================
%attention: The population density is people/(km^2).
%======================================================
b=load('D:\tjpg\matlab_pg\tianjin_caln_popu.txt');
peop=b(:,3);
%==========================================
PGAA=4*10^(-16)*(PGA.^(15));                      %========
peop=PGAA.*peop;                                         %========
men=round(sum(peop));                                  %========
%==========================================
[xx,yy]=meshgrid(116.7:0.02:118.1,38.5:0.02:40.3);
[s1,s2]=size(xx);
uu=NaN(s1,s2);
mm=length(lon2);
for i2=1:1:mm
  j=round( 50*( lon2(i2) -116.7 ) +1);
  i=round( 50*( la2(i2) -38.5 ) +1);
  uu(i,j)=peop(i2);
end
s=shaperead('D:\tjpg\matlab_pg\tj_mp\tianjin.shp');
%===========
%figure;
zft1=figure('units','normalized','position',...
[0.15 0.15 0.75 0.92],'visible','off');
set(gca,'position',[0.20 0.05 0.70 0.90]);   
pcolor(xx,yy,uu);
shading interp;
colorbar;
hold on
hh=mapshow(s,'color','g');
set(hh,'LineWidth',1);
axis([116.65 118.12 38.45 40.35]);
box on
view(0,90);
men=num2str(men);
c1='The totle dead are';
c2=['    ', men,' people'];
men2={c1, c2};
text(117.7,38.6,men2,'FontSize',10,'EdgeColor','b');
caxis([1 20]);
xlabel('Tianjian Earthquake Incited Dead Distribution');
%------------------------------------------------------
clear b;clear peop;clear uu;
%-=====================================================
%attention: The GDP of calculation nodes has not been divided by grid area
%======================================================
b=load('D:\tjpg\matlab_pg\tianjin_caln_eco.txt');             %============
peop=b(:,3);                                                                        %============
PGAB=2*10^(-10)*(PGA.^(11.6));                                      %============
peop=PGAB.*peop;                                                            %============
gdp=round(sum(peop));                                                      %============
peop=peop./(0.02*111.1949)^2;
gdp=num2str(gdp);
%=======================================================
[xx,yy]=meshgrid(116.7:0.02:118.1,38.5:0.02:40.3);
[s1,s2]=size(xx);
uu=NaN(s1,s2);
mm=length(lon2);
for i2=1:1:mm
  j=round( 50*( lon2(i2) -116.7 ) +1);
  i=round( 50*( la2(i2) -38.5 ) +1);
  uu(i,j)=peop(i2);
end

%=====================================================
%=====================================================
%=====================================================
Fpath='D:\tjpg';
CurrentDate = datestr(now,30);         %return date form is (ISO 8601) yyyymmddTHHMMSS
Fpath = [Fpath '\' CurrentDate(1:8)];
if ~exist(Fpath)
    mkdir( Fpath );
end
Fname = CurrentDate(1:13); Fext = '.doc';
Filespec = fullfile(Fpath,[Fname,Fext]);

ReportDoc = actxserver('Word.Application');
if ~exist(Filespec,'file');
  MS = invoke(ReportDoc.Documents,'Add');
else
  MS = invoke(ReportDoc.Documents,'Open',Filespec);
end
set(ReportDoc,'Visible',1);
documents = ReportDoc.Documents;
if exist(Filespec,'file');
    document = invoke(documents,'Open',Filespec);
else
    document = invoke(documents, 'Add');
    document.SaveAs(Filespec);
end

content = MS.Content;
selection = ReportDoc.Selection;
paragraphformat = selection.ParagraphFormat;
document.PageSetup.TopMargin = 60;
document.PageSetup.BottomMargin = 45;
document.PageSetup.LeftMargin = 45;
document.PageSetup.RightMargin = 45;

title='强震台网地震灾情快速评估简报';
set(ReportDoc.Selection,'Text',title);
set(paragraphformat, 'Alignment','wdAlignParagraphCenter');
rr=document.Range(0,14);%选择文本范围
rr.Font.Size=25;%设置文本字体大小
rr.Font.Bold=6;%设置文本字体粗细
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  %回车换行
tt1='年';tt2='月';tt3='日';tt4='(';tt5=')';
tt0=[tt4,CurrentDate(1:4),tt1,CurrentDate(5:6),tt2,CurrentDate(7:8),tt3,tt5];
set(ReportDoc.Selection,'Text',tt0);
set(paragraphformat, 'Alignment','wdAlignParagraphCenter');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  


%men=num2str(men);
tex1='据地震台网中心测定,';
tex2='在东经';
tex3='度,北纬';
tex4='度发生的Ms';
tex5=' 级地震,造成天津地区总死亡人数约为';
tex6='人,GDP损失总数约为';
tex7='万元,具体灾情分布见图1、图2,地震引起的烈度场分布见图3:';
tex=['    ',tex1,t,tex2,x,tex3,y,tex4,mag,tex5,men,tex6,gdp,tex7];
   
set(ReportDoc.Selection,'Text',tex);
set(paragraphformat, 'Alignment','wdAlignParagraphLeft');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');


print(gcf,'-dmeta');
invoke(ReportDoc.Selection,'Paste');
delete(zft1);
tex='        Fig 1. 地震造成的人员死亡分布图(单位:人/平方千米)';
set(ReportDoc.Selection,'Text',tex);
set(paragraphformat, 'Alignment','wdAlignParagraphCenter');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  



%-----------------------------
zft2=figure('units','normalized','position',...
[0.15 0.15 0.75 0.92],'visible','off');
set(gca,'position',[0.20 0.05 0.70 0.90]);
pcolor(xx,yy,uu);
shading interp;
colorbar;
hold on
hh=mapshow(s,'color','g');
set(hh,'LineWidth',1);
axis([116.65 118.12 38.45 40.35]);
box on
view(0,90);

c1='The totle GDP lost is';
c2=['  ', gdp,'0000Yuan'];
gdp={c1, c2};
text(117.65,38.6,gdp,'FontSize',10,'EdgeColor','b');
caxis([100 20000]);
xlabel('Tianjian Earthquake GDP Lost');
print(gcf,'-dmeta');
invoke(ReportDoc.Selection,'Paste');
delete(zft2);
tex='             Fig 2. 地震造成的GDP损失分布图(单位:万元/平方千米)';
set(ReportDoc.Selection,'Text',tex);
set(paragraphformat, 'Alignment','wdAlignParagraphCenter');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  

zft3=figure('units','normalized','position',...
[0.15 0.15 0.75 0.92],'visible','off');
set(gca,'position',[0.20 0.05 0.70 0.90]);
PGA_image2;
print(gcf,'-dmeta');
invoke(ReportDoc.Selection,'Paste');
delete(zft3);
tex='        Fig 3. 地震造成的地震烈度分布图';
set(ReportDoc.Selection,'Text',tex);
set(paragraphformat, 'Alignment','wdAlignParagraphCenter');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');

tex='    报:局领导';
set(ReportDoc.Selection,'Text',tex);
set(paragraphformat, 'Alignment','wdAlignParagraphLeft');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
tex='    送:各有关处、室、部门';
set(ReportDoc.Selection,'Text',tex);
set(paragraphformat, 'Alignment','wdAlignParagraphLeft');
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');
% Close the presentation window:
invoke(MS,'Close');
% Quit MS Word
invoke(ReportDoc,'Quit');
delete(ReportDoc);

评分

1

查看全部评分

发表于 2009-12-10 13:46:45 | 显示全部楼层 来自 浙江杭州
Simdroid开发平台
将图片存为“嵌入型”格式的问题是如何解决的?
回复 不支持

使用道具 举报

发表于 2009-12-10 14:59:39 | 显示全部楼层 来自 北京
1# liushuangq05
给个数据文件,看看你的效果如何
回复 不支持

使用道具 举报

发表于 2009-12-10 18:23:46 | 显示全部楼层 来自 山东烟台
将图片存为“嵌入型”格式的问题是如何解决的?
messenger 发表于 2009-12-10 13:46


复制,然后另起一段粘贴
hgexport(zft, '-clipboard');
selection.TypeParagraph;%另起一个段落
selection.Paste;

评分

1

查看全部评分

回复 不支持

使用道具 举报

 楼主| 发表于 2009-12-10 21:49:39 | 显示全部楼层 来自 天津
print(gcf,'-dmeta');
invoke(ReportDoc.Selection,'Paste');
用这个函数导入到word的图片就是嵌入型的。ZOrder没有嵌入型的设置。

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2009-12-11 11:02:59 | 显示全部楼层 来自 美国
记得matlab里面有个Publish功能,不知道两者有何区别?
回复 不支持

使用道具 举报

 楼主| 发表于 2009-12-11 16:09:25 | 显示全部楼层 来自 天津
应ljelly 要求,把个生成的word贴出来。放附件里。更详细数据暂不公布了。由于图片文件大于100k了,所有转成黑白的pdf贴上来,格式不变动。请见谅。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2009-12-11 22:17:10 | 显示全部楼层 来自 天津
辛苦了,支持加分
回复 不支持

使用道具 举报

发表于 2009-12-12 07:16:07 | 显示全部楼层 来自 山东烟台
怎么那么多
invoke(ReportDoc.Selection,'MoveDown');
invoke(ReportDoc.Selection, 'TypeParagraph');  
弄个for循环不就完了
回复 不支持

使用道具 举报

发表于 2010-8-18 18:21:14 | 显示全部楼层 来自 浙江宁波
这些代码的相关函数matlab的help中可以查到吗?
回复 不支持

使用道具 举报

发表于 2011-1-16 22:14:15 | 显示全部楼层 来自 广西柳州
正要好好学习下

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2011-1-18 15:27:01 | 显示全部楼层 来自 大连理工大学
奇怪,为什么不把代码弄在m文件中上传上来而是粘贴到论坛上来?
回复 不支持

使用道具 举报

发表于 2011-1-18 15:43:42 | 显示全部楼层 来自 北京
楼上,你说为什么大家不把在论坛要讨论的话写在记事本或者word里,然后上传上来呢?每次大家讨论之前先下载,然后写完再上传?
你说的做法容易使得论坛沦为资料中转站,而失去其原有的最根本重要的讨论、交流的特点。
回复 不支持

使用道具 举报

发表于 2011-2-23 13:24:48 | 显示全部楼层 来自 北京
辛苦了。。。

评分

1

查看全部评分

回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-10-5 01:21 , Processed in 0.038485 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表