Control 版 (精华区)
发信人: oli (我笑我痴我癫狂), 信区: Control
标 题: MATLAB与C语言的相互调用
发信站: 紫丁香 (Thu Sep 18 18:59:18 1997)
这是一个HTM文件,有兴趣的朋友可以Paste 下来在Netscape
上读。
<html>
<head>
<title>MATLAB & C</title>
<h1>Using <a href="http://www.indiana.edu:80/~statmath/math/matlab/">MATLAB</a>>
</head><body>
<hr>
<h2>INTRODUCTION</h2>
<p>
This document explains how to call <a href="#eng">MATLAB from a C</a> program,
and how to call a <a href="#mex">C program from MATLAB</a>.
It is assumed you are using
the <a href="http://sckb.ucssc.indiana.edu/kb/data/all.afcm.html">ships</a>
cluster.</p>
<hr>
<a name="eng">
<h2>CALLING MATLAB FROM C</h2>
<p>
The following program will draw one period of the sine funtion:
</p>
<pre>
/* matplot.c -- program to illustrate calling matlab
to display graphical output */
#include <stdio.h>
#include <math.h>
#include "engine.h"
main(){
Engine *ep;
double x[20],y[20];
int i;
/* start MATLAB */
if(!(ep=<a href="http://copper.ucs.indiana.edu/~statmath/matlab/engOpenh
tml">engOpen</a>("\0"))){
fprintf(stderr,"Can't start MATLAB engine.\n");
exit(10);
}
/* make something to plot */
for(i=0;i<20;i++){
x[i]=(float)i/10*3.14159;
y[i]=sin(x[i]);
}
/* give MATLAB the data and plot it */
<a href="http://copper.ucs.indiana.edu/~statmath/matlab/engPutFull.
html">engPutFull</a>(ep,"x",20,1,x,0);
<a href="http://copper.ucs.indiana.edu/~statmath/matlab/engPutFull.
html">engPutFull</a>(ep,"y",20,1,y,0);
<a href="http://copper.ucs.indiana.edu/~statmath/matlab/engEvalString.
html">engEvalString</a>(ep,"plot(x,y,'g')");
printf("Hit return to continue...");
getchar();
/* quit MATLAB */
<a href="http://copper.ucs.indiana.edu/~statmath/matlab/engClose.
html">engClose</a>(ep);
exit(0);
}
</pre>
To compile and link this program on the ships cluster use the
following command:
<pre>
cc matplot.c -o matplot \
-I/usr/local/matlab/extern/include \
/usr/local/matlab/extern/lib/sgi/libmat.a -lm
</pre>
Test the program by running it.
<pre>
$ ./matplot
Hit return to continue...
</pre>
Matlab will start and display the following graph:
<p>
<center>
<img src="matlab_sin.gif">
</center><p>
The directory /usr/local/matlab/extern/src contains more examples
on calling MATLAB from C and FORTRAN.
<p>
<hr>
<a name="mex">
<h2>CALLING C FROM MATLAB</h2>
<p>
The following program will add seven to the first compenent
of its argument and return a 1x1 matrix.
</p>
<pre>
/* addseven.c -- program to illustrate calling C to
to a computation */
#include "mex.h"
void <a href="http://copper.ucs.indiana.edu/~statmath/matlab/mexFunction.html">e
xFunction</a>(nout,dout,nin,din)
int nin,nout;
Matrix *din[],*dout[];{
if(nout!=1){
<a href="http://copper.ucs.indiana.edu/~statmath/matlab/mexErrMg
Txt.html">mexErrMsgTxt</a>("One output argument required.");
return;
} if(nin!=1){
} if(nin!=1){
<a href="http://copper.ucs.indiana.edu/~statmath/matlab/mexErrMg
Txt.html">mexErrMsgTxt</a>("One input argument required.");
return;
}
dout[0]=<a href="http://copper.ucs.indiana.edu/~statmath/matlab/mxCreatF
ull.html">mxCreateFull</a>(1,1,REAL);
*<a href="http://copper.ucs.indiana.edu/~statmath/matlab/mxGetPr.html">x
GetPr</a>(dout[0])=<a href="http://copper.ucs.indiana.edu/~statmath/matlab/mxGeS
calar.html">mxGetScalar</a>(din[0])+7.0;
}
</pre>
To compile and link this program use the following command:
<pre>
cmex -O addseven.c
</pre>
You may ignore the error message
<pre>
Modified: Not found
</pre>
A file called addseven.mexsg should be created in your directory.
Run matlab to test it.
<pre>
$ matlab
< M A T L A B (R) >
(c) Copyright 1984-94 The MathWorks, Inc.
All Rights Reserved
Version 4.2c
Nov 23 1994
Commands to get started: intro, demo, help help
Commands for more information: help, whatsnew, info, subscribe
>> a=addseven(5)
a =
12
>> quit;
No flops.
</pre>
The directory /usr/local/matlab/extern/src contains more example
on calling C and FORTRAN from matlab.
<p>
<hr>
<img src="../minicorn.t.gif" alt="">
<a href="http://www.indiana.edu/~statmath/">
Stat/Math Home Page</a>
<p>
URL http://www.indiana.edu/~swinfo/math/
<br>
Prepared 10 November 1995 by Eric Olson.
<br>
Copyright 1995, The Trustees of
<a href="http://www.indiana.edu/iub/">
Indiana University</a>.
<br>
</body></html>
--
※ 来源:.紫丁香 pclinux.hit.edu.cn.[FROM: 241c05.hit.edu.]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.164毫秒