makemid+matlab,调用matlab compiler编译出来的函数

All programs that call MATLAB Compiler generated shared libraries have roughly the same structure:

Declare variables and process/validate input arguments.

Call mclInitializeApplication , and test for success. This function sets up the global MCR state and enables the construction of MCR instances.

Call, once for each library, Initialize , to create the MCR instance required by the library.

Invoke functions in the library , and process the results. (This is the main body of the program.)

Call, once for each library,Terminate , to destroy the associated MCR.

Call mclTerminateApplication to free resources associated with the global MCR state.

Clean up variables, close files, etc., and exit.

To see these steps in an actual example, review the main program in this example, triangle.c .

m文件

function [x, y] = sierpinski(iterations, draw)

% SIERPINSKI Calculate (optionally draw) the points in Sierpinski's triangle

% Copyright 2004 The MathWorks, Inc.

% Three points defining a nice wide triangle

points = [0.5 0.9 ; 0.1 0.1 ; 0.9 0.1];

% Select an initial point

current = rand(1, 2);

% Create a figure window

if (draw == true)

f = figure;

hold on;

end

% Pre-allocate space for the results, to improve performance

x = zeros(1,iterations);

y = zeros(1,iterations);

% Iterate

for i = 1:iterations

% Select point at random

index = floor(rand * 3) + 1;

% Calculate midpoint between current point and random point

current(1) = (current(1) + points(index, 1)) / 2;

current(2) = (current(2) + points(index, 2)) / 2;

% Plot that point

if draw, line(current(1),current(2));, end

x(i) = current(1);

y(i) = current(2);

end

if (draw)

drawnow;

end

void mlfSierpinski(int nargout, mxArray** x, mxArray** y,mxArray* iterations, mxArray* draw)

第一个参数是返回的参数个数,之后分别为返回参数,输入参数

triangle.c程序为:

/*=================================================================

*

* TRIANGLE.C

*

* Calculate the points in Sierpinski's triangle, an elementary

* fractal.

*

* This function calls a shared library, libtriangle, created

* using MATLAB Compiler.

*

* Copyright 1984-2007 The MathWorks, Inc.

*

*=================================================================*/

#include

/* Include the library specific header file as generated by MATLAB Compiler;

* this includes the MCLMCRRT proxy layer header automatically.

*/

#include "libtriangle.h"

#include "main_for_lib.h"

void usage(const char *name)

{

printf("Usage: %s [number of points]/n");

exit (-1);

}

int run_main(int ac, char **av)

{

/* Input parameters:

*

* iterations: Number of points to draw in the triangle

* draw: If true, draw the triangle in a figure window before returning.

*/

mxArray *iterations = NULL, *draw = NULL;

/* The Sierpinski function returns the X and Y coordinates of the points

* forming the pattern in the triangle.

*/

mxArray *x = NULL, *y = NULL;

/* Default number of iterations */

int num_points = 7500;

/* Validate the number of inputs */

if (ac < 1 || ac > 2)

{

fprintf(stderr, "Expecting 0 or 1 input(s). Found %d/n", ac);

usage(av[0]);

return -1;

}

/* If we have the right number of inputs (1), try to convert the input

* string to an integer.

*/

if (ac == 2)

num_points = atoi(av[1]);

/* Type check on input argument -- atoi() will fail if the input is

* not an integer.

*/

if (num_points == 0)

{

fprintf(stderr, "First argument must be an integer./n");

usage(av[0]);

return -2;

}

/* Call the library intialization routine and make sure that the

* library was initialized properly

*/

if( !mclInitializeApplication(NULL,0) )

{

fprintf(stderr,"could not initialize the application./n");

return -2;

}

if (!libtriangleInitialize())

{

fprintf(stderr,"could not initialize the triangle library properly/n");

return -3;

}

else

{

/* Create the input data */

iterations = mxCreateDoubleScalar(num_points);

draw = mxCreateLogicalScalar(1);

/* Call the library function */

mlfSierpinski(2, &x, &y, iterations, draw);

/* Display the return value of the library function */

printf("Calculated %d points/n", mxGetNumberOfElements(x));

/* Block until user dismisses the figure */

mclWaitForFiguresToDie(NULL);

/* Free the memory used by the return values. */

mxDestroyArray(x); x=NULL;

mxDestroyArray(y); y=NULL;

/* Call the library termination routine */

libtriangleTerminate();

/* Free the memory used by the input variables */

mxDestroyArray(iterations); iterations=0;

mxDestroyArray(draw); draw = 0;

}

/* Nnote that you should call mclTerminate application in the end of

* your application. mclTerminateApplication terminates the entire

* application.

*/

/* Shut down all MCR instances */

mclTerminateApplication();

/* Success */

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值