第十二章 应用程序接口API_第1页
第十二章 应用程序接口API_第2页
第十二章 应用程序接口API_第3页
第十二章 应用程序接口API_第4页
第十二章 应用程序接口API_第5页
已阅读5页,还剩31页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第十二章应用程序接口API

前面章节主要叙述MATLAB自身的各种功能和使用方法。作为优秀软件,MATLAB

不仅自身功能强大、环境友善、能十分有效地处理各种科学和工程问题,而且具有极好的

开放性。这开放性表现在两方面:一,MATLAB适应各科学、专业研究的需要,提供了各

种专业性的工具包;二,MATLAB为实现与外部应用程序的“无缝”结合,提供了专门的

应用程序接口API。遵循本书“淡化专业,面向通用”的宗旨,本章将集中阐述MATLAB

的应用程序接口。本章分五节,各节内容简述如下。

第12.1节集中介绍:如何编写CMEX源码程序,也就是如何为现有的C程序编写接

口程序,使之成为MATLAB函数文件;运用这种技术,读者可以把积累的优秀C程序改

造成可在MATLAB中方便调用的指令。

第12.2节用于演示:如何编写产生MAT数据文件的C源码程序。读者通过算例入

门,就不难借助MAT文件实现MATLAB与外部应用程序的数据交换。

第12.3节围绕MATLAB引擎技术展开。借助这种技术,前台可以是各种外部应用程

序编写的界面,而后台计算则可完全交由MATLAB进行。

第12.4节用三个算例来说明如何应用ActiveX实现MATLAB与外部应用程序的通

信。在第一个算例中,MATLAB用作为客户,服务器是Excelo在后两个算例中,服务器

是MATLAB,而客户是PowerPointo由此产生的PPT文件,可以在放映过程中,实时地

进行MATLAB调用。

第12.5节的内容是:如何借助DDE技术在MATLAB与其他外部程序间进行通信。该

节的一个算例演示:VB划作的界面如何借助DDE建立的对话通道调用服务器MATLAB

进行计算和显示结果图形。而另一个算例则演示:MATLAB如何以客户身份与服务器

Excel建立DDE“热连接”,使MATLAB图形实时地跟随电子表格数据的改变而变化。

值得指出:MATLAB6.0,6.1版用于API的(MEX、MAT、及引擎)库函数许多已经

被废止。本章内容是根据MATLAB6.5编写的。

12.1C语言MEX文件的编写

12.1.1关于MEX文件的一般性说明

12.1.2MEX文件中的MATLAB数据

A=[fabed';'1234';'ABCD']

A=

abed

1234

ABCD

12.1.3C语言MEX文件源程序的构成

【例12.1.3-1]列出具有相同运算功能(实现两个双精度实数标量加法)的C++源强程序

和C++MEX源码程序;对C++MEX源码程序进行编译链接;在MATLAB中调用生成的

DLL文件。通过本例,从感性上认识:(A)一般C源码文件如何改写成具有约定格式的

CMEX源码文件;(B)CMEX源码文件的基本结构;(C)基本的编译链接方法;

(D)DLL文件的调用方法。

(1)

#includc<math.h>

voidmyplus(doubley[l,doublexfLdoublez[])

(

y[0]=x[0]+z[0];

return;

)

(2)

[exm120I3_l.cpp|

#includeHmex.h"//<1>

//

voidniypkis(doubley[],doublex[],doublez[])

(

y[0]=x[0]+z[0];

//-

voidmexFunction(intnlhs,mxArray*plhs[],intnrhs,constmxArray*prhs[])//<8>

(

double*x,*y,*z;//

inimrowsO,ncolsO://

intmrows1,ncols1;//

if(nrhs!=2)//<13>

mcxErrMsgTxt("Twoinputsrequired.");//<16>

elseif(nlhs>I)//<15>

niexErrMsgTxt("Tocmanyoutputarguments");//<16>

mrows0=mxGetM(prhs[0]);//<17>

ncolsO=mxGetN(prhs[OJ);

mrows1=mxGetM(prhs[1]);

ncols1=mxGctN(prhs[1]);//<20>

//

if(!mxIsDouble(prhs[0])||mxIsComplex(prhs[0])||!(mrowsO==l&&ncolsO==l))//<22>

mexErrMsgTxt("Inputsmustbeallnoncomplexscalardouble.1');

//

if(!mxIsDouble(prhs[l])||mxIsComplex(prhs[lJ)||!(mrowsl==l&&ncolsl==l))//<25>

mexEnMsgTxt("Inputsmustbeallnoncomplexscalardouble.");

//

if(mrowsO!=mrows1||ncolsO!=ncols1)//<28>

mexErrMsgTxt("Inputsmustbesamedimens沁n.");//<29>

//

plhs[0]=mxCrcatcDoublcMatrix(mrows0,ncols0,mxREAL);//<31>

x=mxGetPr(prhs[O]);//<32>

z=mxGetPr(prhs[1]);//<33>

y=mxGetPr(plhs[O]);//<34>

myplus(y,x,z);

)

(3)

cdD:\mywork

mexexml2013_l.cpp

direxml2013_l.*

exml213_l.cppexml213_l.dll

(4)

a=0.lll;b=0.222;

c=exml2013_l(a,b)

c=

0.3330

12.1.4CMEX文件的执行流程

12.1.5编写CMEX文件的常用库函数和示例

12.1.5.1常用的MEX库函数

(1)

#include"mex.h"

voidmcxFunctionCintnlhs.mxArray*plhs[],ininrhs,constnixArray*prhs[1)

/*其他C源码*/

I

(2)

#include"mex.h"

voidmexErrMsgTxt(constchar*error_msg);

voidmexWarnMsgTxt(constchar*warning_msg);

(3)

#includc"mcx.h"

inimexCallMATLAB(intnlhs,mxArray*plhs[],intnrhs,

mxArray*prhs[],constchar*command_name);

(4)

#include"mex.h"

intmcxEvalString(constchar"command);

(5)

#includc"mcx.h"

mxArray*mexGetVariable(constchar*workspace,constchar*var_name);

intmexPutVariable(constchar*workspace,constchar*var_name,mxArray*array_p(r);

12.1.5.2常用的MX函数

(1)

#include"matrix.h"

mxArray*mxCreateNumencMatrix(intm,intn,nixClassIDclass,

mxConiplexityComplexFlag);

(2)

#include"matrix.h"

intmxGetM(constmxArray*array_ptr);

intmxGetN(constmxArray*array_ptr);

voidmxSetM(inxArray*array_ptr,intrn);

voidmxSetN(mxArray*array_ptr,intm);

(3)

#include"matrix.h"

double*mxGetPr(conslmxArray*array_ptr);

double*mxGetPi(constmxArray*array_ptr);

voidnixSetPr(mxArray*array_ptr,double*pr);

voidrnxSctPi(mxArray*array_ptr,double*pr);

(4)

#include"matrix.h"

#include<stdlib.h>

void*mxCalloc(size_tn,size_tsize);

12.1.5.3编程中例

【例12.1.5.3-1]创建一个C语言MEX文件,实现对MATLAB两个“单行”字符串的合

并。本例演示:(A)如何根据MATLAB约定的规则编写CMEX源码;(B)如何构成

该文件的调用指令;(C)如何为MEX文件编写在线帮助文件。

(1)

#include"mex.h"//<1>

#include"string.h°//<2>

//

voidstringplus(char*input_bufl),char*input_bufl,char*output_buf)

(

strcat(output_buf,input_buf());

strcat(output_buf,input_buf1);

}

//

voidmexFunclion(intnlhs,mxArray*plhs[],ininrhs,constmxArray*prhs[])//<10>

(

char*input_bufl),*input_bufl,*output_buf;

intbuflen.buflenO,buflen1.status;

if(nrhs!=2)//<13>

mcxErrMsgTxt("Twoinputsinquired.");//<14>

elseif(nlhs>l)//<15>

mexErrMsgTxt("Toomanyoutputarguments.");//<16>

if(mxIsChar(prhs[O])!=1||mxIsChar(prhs[1])!=!)//<17>

mexErrMsgTxt("Inputsmustbeastring.");

if(mxGetM(prhs[O])!=l||mxGetM(prhs[l])!=l)//<19>

mexErrMsgTxt("Inputsmustbearowvector.");

buflenO=(mxGetM(prhs[O])*mxGetN(prhs[O]))+1;//<21>

buflenl=(mxGetM(prhs[l])*mxGetN(prhs[l]))+1;//<22>

buflen=buflenO+buflen1-1;

input_buR)=(char*)mxCalloc(biiflenO,sizeof(char));

input_bufl=(char*)mxCalloc(buflcn1,sizeof(char));

output_buf=(char*)mxCalloc(buflen,sizeof(char));

//

status=mxGetString(prhs[O],input_bufO,buflenO);//<30>

if(status!=O)

mexWamMsgTxt("Notenoughspace,Stringistruncated.");

//

status=mxGetString(prhs[1],input_buf1,buflen1);//<34>

if(status!=0)

mexWamMsgTxt("Notenoughspace,Stringistruncated.");

stringplus(input_bufD.input_bufl,output_buf);

//

plhs[0]=mxCreateString(output_buf);//<39>

return;

}

(3)

cdd:\mywork

mexexml20153_l.cpp

(4)

根据以上分析,就可以写出下列exml2O153」.m文件:

%exml20153_l.mTwostringsarcconcatenatedintoalargerstring.

%Cstr=exml20153_l(Astr,Bstr)把字符串Astr和Bstr水平串联

%Astr被串联的“单行”字符串

%Bstr被串联的“单行”字符串

%Cstr由Astr在前,Bstr在后,串联而成的字符串。

%2002年11月编写

(5)

A=1234*;B=abcd,;

C=exml20153_l(A,B)

c

1234abcd

【例12.1.5.3-2]用C语言编写MEX源码文件,在运行中实现对MATLAB函数的调用,

画出了y=1-e0,3/cos(2r)曲线。本例演示:(A)如何在MEX文件中调用

MATLAB的内建指令:(B)如何在MEX文件中调用月户的自编M文件。

(1)

#include"mex.h"

#dcfincMAX1000

//

voidfill(double*pr,int*pin,int*pn,in(max)

(

inti;

*pm=max/2;

*pn=1;

for(i=0;i<i++)

pr[i]=i*(4*3.14159/max);

)

//

voidmexFunction(intnlhs,mxArray*plhs口,intnrhs,constmxArray*prhsf])

(

intm,n,max=MAX;

mxArray*rhs[l],*lhs|l];

ihs[0]=mxCreateDoubleMatrix(max,1,mxREAL);

fill(mxGetPr(rhsfO]),&m.&n,MAX);

mxSetM(rhs[0],m);

mxSetN(rhs[0],n);

mcxCallMATLAB(l,Ihs,1,rhs,"mexzzy");

mexCallMATLAB(O,NULL,1,Ihs,"plot");

mxDestroyArray(rhs[0]);

mxDestroyArray(lhs[0]);

return;

(2)

cdd:\mywork

mexexml20153_2.cpp

(3)

exml20153_2

图12.1-2

12.2MAT数据文件的应用

12.2.1数据的输入输出方法

12.2.2创建MAT文件的C源码程序的编写

【例12.2.2-1】目标:用C++编写一个可创建MAT文件的独立应用程序exml2022_l.exe。

通过该例演示:(A)可创建MAT文件的独立应用程序的编写步骤;(B)相应C++源程

序的基本格式;(C)相应mx-函数和C指令的配合应用。(D)MAT库函数matClose,

matGetArray,matOpen,matPutArray,matPutArrayAsGlobal的使用方法;

(1)

#include<stdio.h>

#include"mat.h"

#include<string.h>

#defineBUFSIZE255

//

intcrcatc(constchar*filc)

//

MATFile*pmat;

mxAiTay*pal,*pa2,*pa3;

doubledata[9]={1.0,4.0,7.0,2.0,5.0,8.0,3.0,6.0,9.0};

charstr[BUFSIZE];

printf("Crcating...\n\n\file);

pmat=matOpen(file,"w");

if(pmat==NULL)

{

prints"Errorcreating\n",file);

printf("(doyouhavewritepermissioninthisdirectory?)"");

return(l);

)

pal=mxCreateDoubleMatrix(3,3,mxREAL);

mxSetCIassName(pa1,"LocalDouble");

pa2=mxCreateDoubleMatrix(3,3,mxREAL);

mxSctClassNamc(p<i2,"GlobalDoublc");

mcmcpy((void*)(mxGetPr(pa2)),(void*)data,sizcof(data));

pa3=mxCreateString("MATLAB:thelanguageoftechnicalcomputing");

mxSetClassName(pa3,"LocalString");

matPutVariable(pmat,'LocalDoubie",pal);

matPutVariableAsGlobal(pmat,"GlobalDouble",pa2);

inatPutVariablc(pmat,"LocalString",pa3);

memcpy((void*)(mxGetPr(pa1)),(void*)data,sizeof(data));

matPutVariable(pmat,'LocalDouble",pal);

mxDestroyAiTay(paI);

mxDcstroyArray(pa2);

mxDestroyArray(pa3);

if(matClose(pmat)!=0)

(

printf("Errorclosing\n",file);

return(l);

1

//

pmat=malOpen(flle,7");

if(pmat==NULL)

printf("Errorreopening\ii",file);

rcturn(1);

1

pal=ma(GetVariable(pmal,"LocalDouble");

//

if(paI==NULL)

{

printf("ErrorreadingexistingmatrixLocalDouble\n");

return(1);

1

if(mxGetNumberOfDimensions(par)!=2)

{

prinlfC'Errorsavingmatrix:resultdoesnothavetwodimensions\n");

return(1);

I

pa2=matGctVariablc(pmat,"GlobalDoublc");

//

if(pa2==NULL)

(

printf("ErrorreadingexistingmatrixGlobaiDoub)e\n");

return(1);

}

if(!(mx!sFromGlobalWS(pa2)))

{

printf("Errorsavingglobalmatrix:resultisnotglobahn");

return(1);

I

pa3=matGetVariable(pmat,"LocalString");

//

if(pa3==NULL)

(

printfC'ErrorreadingexistingmatrixLocalString\n");

return(1);

)

mxGctString(pa3,str,255);

if(strcmp(str,"MATLAB:thelanguageoftechnicalcomputing"))

printf("Errorsavingstring:resulthasincorrectcontents\n");

return(l);

)

mxDcstroyArray(pa1);

mxDestroyArray(pa2);

mxDestroyArray(pa3);

if(matClose(pmat)!=0)

{

printf("Errorclosing\n",file);

return(1);

1

printf("Done\nn);

return(O);

I

//主程序

intmain()

(

iniresult;

result=createC'mattesi.mat");

return(result==O)?EXIT_SUCCESS:EXIT_FAILURE;

}

(2)

cdd:\mywork

mcc-pexml2022_l.cpp

(3)

clear

cdd:\mywork

!exml2022_l

Creating...

Done

loadmattest,mat

who

Yourvariablesare:

GlobalDoubleLocalDoubleLocalstring

GlobalDouble,LocalDouble,LocalString

GlobalDouble=

123

456

789

LocalDouble=

i2a

456

789

LocalString=

MATLAB:thelanguageoftechnicalcomputing

12.3MATLAB引擎技术的应用

12.3.1MATLAB引擎概念和功用

12.3.2引擎库函数及C源码应用程序的编写

【例12.3.2-1]用C语言编写调用MATLAB引擎计算三次多项式/一21十5根的源程

序。

(1)

#include〈windows.h>

#include<stdlib.h>

#include<stdio.h>

#include"engine.h"

intPASCALWinMain(HANDLEhlnstance,HANDLEhPrcvInstance,

LPSTRIpszCmdLine,intnCmdShow)

(

Engine*ep;

mxArray*P=NULL,*r=NULL;

charbuffer[3Ol];

doublepoly[41={1,0,-2,5);

if(!(ep=engOpen(NULL))){

fprintf(stdcrr,"\nCan'tstartMATLABcnginc\nu);

returnEXIT_FAILURE;}

P=mxCreateDoubleMatrix(1,4,mxREAL);

mxSetClassName(P,"p");

mcnicpy((char*)mxGctPr(P),(char*)poly,4*sizcof(double));

engPulVariable(ep,P);

engOu(putBuffer(ep,buffer,300);

engEvalString(ep,"disp(1多项式:poly2str(p,'x»的根]),r=roots(p)");

MessageBox(NULL,buffer,"exm12032_l展示MATLAB引擎的应用",MB_OK);

engClose(ep);

mxDcstroyAnay(P);

returnEXIT_SUCCESS;

1

(2)

cdd:\mywork

mex-fD:\MATLAB6p5\bin\win32\mexopts\msvc60engmatopts.batexml2032_l.c

(3)

exm1232_1展示MATLAB引擎的应用

多项式x“3-2x+5的根

r=

-2.0946

1.0473+1.1359i

1.0473-1.1359i

确定二二|

图12.3-1

【例12.3.2-2]MATLAB引擎综合应用实例。本例演示:(A)MATLAB引擎对用户自编

M函数文件的调用;(B)借用DOS界面作为MATLAB的指令输入和结果发布窗。

(1)

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

#include"engine.h"

#defineBUFSIZE512

intmain()

(

Engine*cp;

mxArray*Pz=NULL,*result=NULL;

charbuffer[BUFSIZE];

doublezeta[41={0.2,0.4,0.8,1.2};//MATLAB环境外数据示例

if(!(ep=engOpen("\0")))〃开启木地MATLAB引擎,如失败给出警告。<12>

(

fprintf(stden\"\nCan'tstartMATLABengine\n");

returnEXIT_FAILURE;

}

//

〃程序段1:

//

Pz=mxCreateDoubleMatrix(1,4,mxREAL);

mxSctClassNamc(Pz,"z");

mcmcpy((void*)mxGctPr(Pz),(void*)zeta,sizcof(zcta));

cngPutVariablc(cp.Pz);

engEvalString(ep,"engzzy(z);n);//<25>

primf(“按Enter键继续!\n\n");

fgetc(stdin);

printf("程序段I运行已经结束。下面处于程序段2运行过程中!\n");

mxDestroyArray(Pz);

cngEvalString(cp,"close;");

//

〃程序段2:

//

//

//

engOutputBuffer(ep,buffer,BUFSIZE);

while(result==NULL){

charstr[BUFSIZEJ;

printf("注意:\n");

printfC•此界面上,可输入任何MATLAB指令。\n");

printfC•若想退出,请对Exit变量赋任何数值。5”);

printf("»");

fgets(str.BUFSIZE-1,stdin);

engEvalString(ep,str);

printf("%s",buffer);

if((result=engGetArray(ep."Exit"))==NULL)

printf("可继续运行!\n");

}

printf("运行结束!\n");

mxDestroyArray(result);

engClose(ep);

returnEXIT_SUCCESS;

)

(2)

cdd:\mywork

mex-fD:\MATLAB6p5\bin\win32\mexopts\msvc60engmatopts.batexml2032_2.c

(3)

图12.3-2

目命令提示符

D:\mywork>exm12032_2

按Ente「键继续!

程序段1运行己经结束。下面处于程序段2运行过程中!

注意.

・而界面上,可输入任何出TLAB指令。

・若想退出,请对Exit变量赋任何数值。

»rand(Fstate,,1)>D=eig(rand(3,3))

D=

2.0361

0.3040

-0.5244

可继续运行!

注意:

・血界面上,可输入任何MATLAB指令。

•若想退出,请对Exit变量赋任何数值。

»Exit=3

Exit=

3

运行结束!

D:\mywork>.

_LU

图12.3-3

12.3.3利用VC++6.0集成环境编写MATLAB引擎程序

【例12.3.3-1]利用VC++6.0集成编程界面编写综合运用MAT数据文件和MATLAB引擎

技术的(求矩阵的奇异值)C+十源码程序。本例演示:(A)如何利用VC++6.0集成编程

界面编写源码程序;(B)编译链接产生EXE文件所需的间夹文件。

(1)

New

FilesProjects|Workspaces|OtherDocuments|

④ATLCOMAppWizardProjectname:

期ClusterResourceTypeVZizard|cxm12033_1-

部CustomAppWizard

DatabaseProjectLocation:

^)DevStudioAdd-inWizard

|DAMYWORK\exml2033_l/

ExtendedStoredProcWizard

ClISAPIExtensionWizard

©Makefile

私MATLABProjectWizard

霸MFCActiveXControlwizardGCreatenewworkspace

画MFCAppWizard(dll)「Addtocurrentworkspace

另MFCAppWizard(cxc)

厂[dependencyof:

J<NewDatabaseWizard

TlUtilityProject

~?1Win32ApplicationIz

:Win32ConsoleApplication

Win32DynamicLinkLibrary

>|Win32StaticLibraryplatforms:

MWin32

OK|Cancel

图12.3-4

Win32ConsoleApplication-Step1of1a凶

WhatkindofConsoleApplicationdoyou

wanttocreate?

6Anemptyprojects

「Apimpleapplication.

CA"Hello.Worldfapplication.

「AnapplicationthatsupportsMFC.

<且ackNext>IFinishCancel

图12.3-5

New-ZJxJ

FilesProjects|OtherDocuments|

gjActiveServerPagePAddtoproject:

回BinaryFile

|exm12033_1J

箜BitmapFile

口C/C**HeaderFile

圆C++SourceFile

除CursorFileFile

HTMLPage

二IconFile|exm12033_1

居MacroFile

^ResourceScriptLocation:

^ResourceTemplate|DAMYW0RK\exm12033_1J

园SQLScriptFile

国TextFile

OKICancel

图12.3-6

♦.exml2033」-MicrosoftVisualC++-[exml2033l.cpp"],|a|x|

曲FieEditViewInsertft^ojectBuildToolsWindowHelp-|(9|x|

:窗东R第%

|(Globals)▼|(Allglobalmembers二Jmain二]国,

修圜善!ffll四

//定义B*r-

ttinclude"engine.h"MATL

k/定义z

MAr

ttinclude鬻

o士

/定义

唱Workspace'exml2033_1':/H"

ttinclude<iostrean.h>雪^

/定义

/通ir

B国cxm12033_1filesttinclude<windows.h>

//定义-

*6SourceFilesttinclude<stdlib.h>和

//定义nt

画exm12033」.cpjttinclude<iomanip.h>?

口HeaderFilesuoidnain()

LJResourceFiles

double*b,a[9]=<1,4,7,2,5,8,3,6,9);

constchar»File="nynat-mat";

mxArray*Ain,*Aout,*SU;

Engine*ep;

<|I2JMATFile«mat;

数据写年

■2Class%工]圜FileView「

IBU

ReadyLn2,Col29|REC|COL|OVR|READ/

图12.3-7

(2)

#includc"engine.h"

#include"inat.h"

#include<iostreani.h>

#include〈windows.h>

#include<stdlib.h>

#include<iomanip.h>

voidmain()

{

double*b,a[9]={1,4,7,2,5,83,6,9);

constchar*filc="myniat.matH;

mxArray*Ain.*Aout,*SV;

Engine*ep;

MATFile*mat;

//

mat=matOpen(file,"\v");

Ain=mxCrcatcDoublcMatrix(3,3,mxREAL);

inxSetClassName(Ain,"z");

niemcpy((char*)mxGctPr(Ain),(char*)a,9*sizeof(double));

matPutVariable(mal,"z",Ain);

matClosc(mat);

mxDestroyArray(Ain):

//

mat=matOpen(file,"r");

Aout=matGetVariable(mal,,,z,');

if(ep=cngOpen(NULL))

{

cngPutVariablc(cp,Aout);

engEvalString(ep,"sv=svd⑵丁');

SV=engGelVariable(ep,,,sv");

b=mxGetPr(SV);

couiw”奇异值为";

cout«"\n";

for(inti=0;i<3;i++)

{

cout«setw(16)«b[i];

)

engClose(ep);

matClose(mat);

mxDestroyArray(Aou();

mxDestroyAiray(SV);

else

cout«"Can'topenmatlab";

}

图12.3-8

(3)

(4)

(5)

图12.3-9

12.4MATLAB中ActiveX技术的应用

12.4.1关于ActiveX的一般性说明

12.4.2MATLAB的ActiveX自动化

12.4.2.1MATLABActiveX自动化控制器

【例12.4.2”】MATLAB以自动化客户的资格通过M脚本文件把MicrosoftExcel用作自动

化服务器。本例演示调用自动化服务器的基本指令:Excel缺省界面的开启;增添工作簿

(Workbook);改变激活的当前页(Worksheet);MATLAB与Excel之间的数据传递;

Excel的数据保存。

%exm!2042_l.m

excel=actxserver('Excel.Application');

%

%

dispf为看清Excel界面及其变化,请把MATLAB界面调整得远小于屏幕!)

dispC按任意键,将可见到“Excel界面”出现J)

pause

set(excel,'Visible*,1);

dispC按任意键,可见到Excel界面出现第一张表激活的“空白工作簿”。,)

pause

wkbs=excel.Workbooks;

Wbk=invoke(wkbs,'Add');

disp(按任意键,当前激活表由第一张变为指定的第二张J)

pause

Sh=excel.ActiveWorkBook.Sheets;

sh2=get(Sh,'Itein\2);

invoke(sh2,'Activate');

dispC按任意键,把MATLAB空间中的A矩阵送到Excel的指定位置。)

pause

Actsh=excel.Activcshcct;

A=[l,2;3,4];

actshrng=gct(Actsh,'Rangc',"A1','B2");

set(actshmg,'Value',A);%<23>

dispf按任意键,获取Excel指定区域上的数据,,)

disp('并以MyExcel.xls文件形式保存在D:\mywork目录上。’)

pause

rg=gct(Actsh,'Range','Al','B2');

B=rg.value;

B=reshape([B{:}],size(B));

invoke(Wbk,'SaveAs','D:\mywork\my');

dispC按任意键,关闭excel句柄代表的Excel(,')

pause

invoke(excel,'Quit');

图12.4-1

12.4.2.2MATLABActiveX自动化服务器

【例12.4.2.2-1]创建一个名为cxml20422.ppt的演示文稿。它包含一张如图12.4-2所示的

幻灯片。该幻灯片在放映时,若在左上方空白框中输入任何MATLAB的合法指令,尔后

按下计算按键,则在下方空白框中能实时地显示运算结果,如图12.4-lOc

计算

图12.4-2

(1)

PowerPoint

c打开已有的演示文稿©)

图12.4-3

图12.4-4

图12.4-5

(2)

图12.4-6

图12.4-7

图12.4-8

(4)

PrivateSubCommandButtonl_Click()

EndSub

图12.4-9

Rem

DimhAsString

DimresultAsString

Rem

DimmatlabAsObject

Setmatlab=CreateObject("Matlab.Applicat沁n")

Rem

h=TextBoxl.Value

Rem

result=inatlab.Execute(h)

Rem

TextBox2.Value=result

(5)

clc,rand('state',1)

A=rand(3,3),Cond_A=cond(A);

disp('TheconditionnumberofA')计算

disp(Cond_A)

A=

0.95280.59820.8368

0.70410.81070.5187

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论