(有偿)有人做过matlab c混编吗,matlab调用c程序,c传出三维矩阵
下面是C的程序,在matlab里面调用的时候,也不报错,就是直接闪退,我查了网上说可能是数组溢出了,断点调试了,但是也没找到是哪有问题#include "mex.h"
#include "math.h"
void Recon(double *dims, double *m, double *Pro, double *P)
{
int i, j, k;
int u, v;
double pu, pv, lamda, x, y, z, dx, dy, dz;
double inter_x, inter_y ,sin_w,cos_w ,s;
double Nx, Ny, Nz, nu , nv ,light_to_center;
Nx = dims;
Ny = dims;
Nz = dims;
dx = dims;
dy = dims;
dz = dims;
nu = dims;
nv = dims;
sin_w = dims;
cos_w = dims;
light_to_center = dims;
for (k = 1; k < Nz+1; k++)
{
for (i = 1; i < Nx+1; i++)
{
for (j = 1; j < Ny+1; j++)
{
x = (i-0.5)*dx - dx*(Nx / 2 + 0.5);
y = (j-0.5)*dy - dy*(Ny / 2 + 0.5);
z = (k-0.5)*dz - dz*(Nz / 2 + 0.5);
//加权参数
//s = - x * sin(deg2rad(beta)) + y * cos(deg2rad(beta));
s = -x * sin_w + y * cos_w;
pu = m * x + m * y + m * z + m;
pv = m * x + m * y + m * z + m;
lamda = m * x + m * y + m * z +1;
inter_x = pu /lamda ;
inter_y = pv /lamda ;
u = int(inter_x); // exchange u,v
v = int(inter_y);
if ((inter_x >= 1 && inter_x <= (nu-1 )) && (inter_y >= 1 && inter_y <= (nv-1)))
{
//在投影图中插值获得当前点的像素
P = Pro * (light_to_center * light_to_center)/ ((light_to_center - s )* (light_to_center - s ));
}
else
{
P= 0;
}
// if ((u >= 1 && u <= (Ni-1 )) && (v >= 1 && v <= (Nj-1)))
// {
// P = P + invoxel * (u - inter_x + 1)*(v - inter_y + 1);
// P = P + invoxel * (u - inter_x + 1)*(inter_y - v);
// P = P + invoxel * (inter_x - u)*(v - inter_y + 1);
// P = P + invoxel * (inter_x - u)*(inter_y - v);
// }
}
}
}
}
//接口
//nlhs 为输出参数的数目,
//plhs是输出参数数组,指针数组
//nrhs是为输入参数的数目
//prhs是输入参数数组吗,是一个指针数组
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
double *dims;
double *m, *Pro;
double *P;
if (nrhs != 3)
mexErrMsgTxt("3 inputs required.");
if (nlhs != 1)
mexErrMsgTxt("One output required.");
dims = mxGetPr(prhs);
m = mxGetPr(prhs);
Pro = mxGetPr(prhs);
/*set the output pointer to the output matrix */
// plhs = mxCreateDoubleMatrix(dims,dims, mxREAL);
const mwSize ds = {*dims,*(dims +1),*(dims +2)};
plhs = mxCreateNumericArray(3,ds,mxSINGLE_CLASS,mxREAL);
/*create a C pointer to a copy of the output matrix */
P = mxGetPr(plhs); //获得矩阵第一个元素的指针
/*call the C subroutine */
Recon(dims, m, Pro, P);
}
//plhs:指向输出参数的指针
//nrhs:输入参数数目
页:
[1]