I'm sure many of you have resolved this MATLAB issue. Finally, I was able to run my MATLAB generated DLLs from LABVIEW. I had to create a wrapper in C to interface the two programs, since my LABVIEW program used type double and MATLAB used type MxArray. Mathworks Note 27671 shows how to create MATLAB DLLs and reference them in C. Two other documents on the National Instruments site show how use DLLs created in C or C++ with LABVIEW. They are:
"Writing Win32 Dynamic Link Libraries(DLLs) and calling them from LABVIEW"
"How to build a DLL with Visual C++"
In the end I had to create 2 DLLs, one from MATLAB and another from Visual C++ (created by compiling the wrapper file to a .DLL file). I've cut & pasted a simple wrapper file below. This simple template with the documents referred to above should answer most questions. To find explanations of matlab functions used such as mxCreateDoubleArray, refer to:
http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/apiref.shtml
Good luck
-Daidipya
// dllarraytest.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "foolib2.h"
#include "matlab.h"
#include
__declspec(dllexport) double wrapper_main(double *,double *,double *,double *,unsigned long);
void fill(double *,double *,int);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
__declspec(dllexport) double wrapper_main(double *in1,double *in2,unsigned long size){
mxArray *in1_ptr,*in2_ptr,*out1_ptr,*out2_ptr;
/* Create an mxArray to input into mlfFoo */
in1_ptr=mxCreateDoubleMatrix(size,1,mxREAL);
in2_ptr=mxCreateDoubleMatrix(size,1,mxREAL);
/*copy values from double array to mxArray*/
fill(mxGetPr(in1_ptr),in1,size);
fill(mxGetPr(in2_ptr),in2,size);
/* Call the library initialization function */
foolib2Initialize();
/* Call the implementation function (the DLL created by foo2.m)*/
out1_ptr = mlfFoo2(&out2_ptr,in1_ptr,in2_ptr);
/* Call the library termination function */
foolib2Terminate();
/* The return value from mlfFoo is an mxArray so we must extract the data from it */
fill(in1,mxGetPr(out1_ptr),size);
fill(in2,mxGetPr(out2_ptr),size);
/*this does not work, memory that I allocated in C would cause
labview to crash*/
//memcpy(out1,mxGetPr(out1_ptr), sizeof(double)*size);
//memcpy(out2,mxGetPr(out2_ptr), sizeof(double)*size);
return size;
}
/* subroutine for filling up data */
void fill(double *out,double *in,int size)
{
int i;
/* you can fill up to max elements, so (*pr)<=max */
for (i=0; i < size; i++)
out[i]=in[i];
}