0
在下面的代碼中,「h_fir」給出了與等價Matlab代碼不同的所有零。Mex-call-to-matlab-firls-do-not-work
「L」= 204621
這是mex文件中的函數。來自Matlab的調用運行良好,並且恢復良好。
你能想出原因嗎?
Matlab的2015A
下面一個小例子。
感謝
路易斯·貢薩爾維斯
#include "C:\Program Files\MATLAB\MATLAB Production Server\R2015a\extern\include\mex.h"
#include "C:\Program Files\MATLAB\MATLAB Production Server\R2015a\extern\include\matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
#include <time.h>
#include <string.h>
#define MAX(p,q) ((p>q)?p:q)
void example(mxArray *,double, double);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double v,f_simb,ts,vkmh,fd,c,R,p,q,fc,pqmax;
mxArray *h_fir;
int g2;
double *a3,*a4;
ts=10e-3;
vkmh=50;
c=3e8;
fc=2e9;
f_simb=1/ts;
v=vkmh/3.6;
fd=v/c*fc;
R=f_simb/(fd*110.5);
p = 100.0;
q = (double)(int)(p/R+0.5);
if (q<0.001)
q= 1.0;
h_fir= mxCreateDoubleMatrix(1,(int)(2.0*10.0*MAX(p,q) + 1.5),mxCOMPLEX);
example(h_fir,p,q);
a3 = mxGetPr(h_fir);
a4=mxGetPi(h_fir);
for(g2=102306-10;g2<102306+10;g2++)
mexPrintf("%e %e\n",a3[g2],a4[g2]);
return;
}
void example(mxArray *h_fir,double p, double q)
{
double N,pqmax,fc,L;
mxArray *L1,*ARRAY1,*ARRAY2;
double *array1,*array2,*hr,*hi;
mxArray *ppFevalRhs[3];
mxArray *ppFevalRhsin[1], *OUT1;
int i1,i2,*l1;
const size_t dims[2]={1,1};
N = 10.0;
pqmax = MAX(p,q);
fc = 1.0/2.0/pqmax;
L = 2.0*N*pqmax + 1.0;
L1 = mxCreateNumericArray(2, dims,mxINT32_CLASS, mxREAL);
l1 = (int *)mxGetData(L1);
*l1= (int)(L-1);
ppFevalRhs[0]=L1;
ARRAY1 = mxCreateDoubleMatrix(4, 1, mxCOMPLEX);
array1 = mxGetPr(ARRAY1);
array1[0]= 0;
array1[1]= 2.0*fc;
array1[2]= 2.0*fc;
array1[3]= 1;
array1 = mxGetPi(ARRAY1);
array1[0]= 0;
array1[1]= 0;
array1[2]= 0;
array1[3]= 0;
ppFevalRhs[1]=ARRAY1;
ARRAY2 = mxCreateDoubleMatrix(4, 1, mxCOMPLEX);
array2 = mxGetPr(ARRAY2);
array2[0]= 1;
array2[1]= 1;
array2[2]= 0;
array2[3]= 0;
array2 = mxGetPi(ARRAY2);
array2[0]= 0;
array2[1]= 0;
array2[2]= 0;
array2[3]= 0;
ppFevalRhs[2]=ARRAY2;
ppFevalRhsin[0]=h_fir;
if (mexCallMATLAB(1, ppFevalRhsin, 3, ppFevalRhs, "firls")!=0)
mexPrintf("firls error\n");
mxDestroyArray(L1);
mxDestroyArray(ARRAY1);
mxDestroyArray(ARRAY2);
}
沒有人會回答嗎? –
從mex調用的matlab函數的輸出變量是由matlab創建的,不需要在之前進行分配。傳遞給matlab的先前分配的指針被matlab覆蓋。這就是爲什麼上面的代碼不起作用。 –