LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calling Matlab DLL From Labview

My goal is to use the Call Library Function Node to call a C-wrapped Matlab function and get a valid result.  I've been able to compile the wrapper in Visual Studio successfully and generate a new DLL.  The Call Library Function Node successfully attaches to the generated DLL and calls the wrapper function, but the Matlab MCR fails to initialize within the function.  I'm posting here because I feel like I've exhausted my options.  I'm just wondering if there is a known issue with initializing the Matlab MCR (runtime engine) from Labview.  I verified the generated DLL works when I create a simple Windows application to call the wrapper function.  

 

Any help would be greatly appreciated.  

0 Kudos
Message 1 of 11
(7,998 Views)

Hello Ian,

 

This issue has been discussed at length elsewhere on the forums. There are a number of reasons why this may occur so taking a look at this thread may have the answer you are looking for.

 

Can you post what version of everything you're using? (visual studio, MCR, compiler, LabVIEW, matlab)

 

Have you tried creating a simple test dll that contains a single function that adds two numbers together? If you have and this reproduces the error you are seeing can you post your dll? 

 

You mentioned that the MCR fails to initialize, are you getting an error message that states this or is the LabVIEW program continuing on as if nothing ever executed? 

 

I would also like to point out the Mathscript Node and the Matlabscript Node are options available within LabVIEW that may accomplish the same goal. Have you had the oppurtunity to evaluate either of these options rather than using a dll?

 

Finally, is this issue only occurring when being accessed from a LabVIEW executable or does this only occur in the LabVIEW development environment or possibly both?

 

0 Kudos
Message 2 of 11
(7,963 Views)

I've tried searching the forums quite a bit.  I'm using Labview 8.5, Matlab 2013a (with latest MCR), and Visual C++ 2008.  

 

I included all the files I created to do this test.  It's messy, but I'm just trying to prove that it's functional.  I created a wrapper DLL that calls multMatrix from foolib.dll (a matlab compiled DLL) to do a matrix multiply.  I verified the wrapper DLL works by making a simple Windows application that calls the wrapper DLL (matlabCWrapperDll.dll) and prints the correct output onto a console.  However, when I run test.vi it exits early when I call mclInitializeApplication() in the wrapper DLL.  I'm certain this is the case because I verified the error code that gets return in the VI--I even changed the return value and verifed just to double check. 

 

I have read some literature about the Mathscript and Matlabscript.  One of our requirements is the target machine can only have the Matlab runtime library and not the full software.  The other is we need to support functions in Matlab that may not be supported in the Labview Mathscript compiler.  Our development team will be using Matlab for the forseeable furture so exporting functions through DLL presents the best solution.  

 

Really appreciate the help.

 

Ian

 

 

0 Kudos
Message 3 of 11
(7,953 Views)

Hi Ian,

 

Nothing is jumping out at me on why it isn't working. As a point of clarification did you create a MATLAB® dll and create a C wrapper for that dll or did you compile your program directly into a C dll? Regardless of which one you used, do you know what mex you used to compile the initial dll? 

 

 

0 Kudos
Message 4 of 11
(7,916 Views)

I created a DLL using the Matlab compiler toolbox and an .m file.  I then created a wrapper DLL by compiling in Visual Studio.  Since I'm trying to execute this code outside of Matlab I don't think I need a mex file, correct?  

 

Thanks,

Ian

0 Kudos
Message 5 of 11
(7,909 Views)

You may not need to create a wrapper depending on how to compiled it. Do you know what mex -setup option you selected when you compiled your initial dll?

0 Kudos
Message 6 of 11
(7,897 Views)

Hello, wondering if a resolution was ever reached?

I'm having a similar issue. My case is that I have several DLLs wrapped in Matlab. Using the library importer in LabVIEW each DLL is rendered in a separate library. The function of interest from each DLL can be ran individually, meaning that I need to kill LabVIEW after each DLL function runs (thet'd run fine). As soon as I attempt to call functions from different DLLs one after the other, the first one executes fine, any subsequent function will not execute. The first one that ran will still run correctly is rerunning it, which makes me think that it has grabbed and hold on to the Matlab Runtime Engine. 

The Matlab RT version in my deployment PC is the same as the Matlab development. Using the same bitness also.

Any ideas from anybody?

0 Kudos
Message 7 of 11
(2,369 Views)

duplicate

Rolf Kalbermatter
My Blog
0 Kudos
Message 8 of 11
(2,356 Views)

Matlab DLLs/shared libraries usually provide various support functions and a high level function for your algorithme. The high level function initializes the runtime, loads any dependencies and what else and then runs the actual algorithme.

And yes your suspicion that the first shared library gets hold of the runtime and the others can't get access to it anymore are correct. What you would have to do is to call the lower level functions yourself that initialize the runtime and prepares all the variables for each shared library and then calls the low level algorithme functions directly. It's a pretty advanced low level operation that almost certainly will require you to write some wrapper code in real C(++) to do this advanced level programming. Not because you couldn't call all these functions from LabVIEW either, but because you would need to understand a lot more about C programming to get the Call Library Nodes all correctly configured than writing that code in C directly. And no, the import library wizard can not do this for you, the Matlab data structures are to complex and the memory management to involved for an automated tool to do this, based on just the information provided in the header file syntax.

Rolf Kalbermatter
My Blog
0 Kudos
Message 9 of 11
(2,354 Views)

Hi Rolfk,

 

Thanks for the quick reply. Would it be correct to assume that if I keep the current LabVIEW libraries created when the library wizard was used, and I install the Matlab Development in the PC where this code is ultimately going to run, this problem will go away, or would it persist regardless?

 

Thanks.

 

JR

0 Kudos
Message 10 of 11
(2,323 Views)