LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Connecting to multiple Matlab instances?

Hi,

 

we need to use Matlab and some of its toolboxes within a Labview project and would like to parallelize our system.

 

As far as I can tell all Matlab script nodes connect to the same Matlab instance that is started when the VI is opened, and each Matlab script node seems to to block others from executing until it's done.

We'd like to have multiple instances of Matlab running and connected to different Matlab script nodes such that two Matlab script nodes (connected to two different Matlab instances) can run in parallel.

 

A very useful way to implement this would be to have a "Create Matlab session" VI, which creates a reference, which is an input to the Matlab script node. That way the user can have multiple Matlab script nodes connecting to different Matlab sessions (decided at design time).

 

I realize that your focus is on improving Mathscript rather than adding functionality to the Matlab interaction, but at this point Mathscript is just not a viable option for many applications.

 

Thanks!

Ulf

0 Kudos
Message 1 of 8
(5,917 Views)

The LabVIEW Matlab script node communicates with Matlab via Matlab's ActiveX server. While you can open up multiple instances of Matlab via ActiveX, the specific implementation in LabVIEW limits it to just one. Thus, LabVIEW would need to be changed to support this.

 

An alternative is to use ActiveX directly to talk to Matlab, bypassing the Matlab script node. And before you ask, no I don't have any examples to do this. I would suggest looking in the Mathworks web site for examples. you will likely find VB examples, but since it's ActiveX they should be transatable to LabVIEW.

0 Kudos
Message 2 of 8
(5,909 Views)

Thank you for the quick reply!

 

I tried your suggestion, and I think there's a way to make it work. The problem right now is that interface that Matlab exposes through the type library they install is only for a "shared server", i.e. one matlab instance serving all clients - the same model that the Matlab script node uses. In order to start "dedicated servers", I need to create a .COM instance by ProgID, just like the CreateObject function in VB.NET.

 

Here's an example. For the shared server, in VB.NET I'd call

 

MatLab = CreateObject("Matlab.Application")

 

For a dedicated server this would be

 

MatLab = CreateObject("Matlab.Application.Single")

 

How can I do this in Labview? Using the Automation Open VI I have to select a library from a list (and it's not in there), or select a file, but I can't find any way to open an instance by the program identifier?

 

Thanks!

Ulf

0 Kudos
Message 3 of 8
(5,903 Views)

Here's some sample code from three years ago for how to achieve this in C#, maybe it's easier to port this to Labview?

I've looked into it, but I haven't found a way to call static functions (Type.GetTypeFromProgID or Activator.CreateInstance) in Labview.

 

 

Type matlab = Type.GetTypeFromProgID("Matlab.Application.Single");
object matlabObject = Activator.CreateInstance(matlab);

// Invoke method PutFullMatrix or any other method
// Here "parameter" should be Object array containing arguments to the method being invoked
matlab.InvokeMember("PutFullMatrix", System.Reflection.BindingFlags.InvokeMethod, null, matlabObject, parameter);
0 Kudos
Message 4 of 8
(5,878 Views)

I did some more research and finally found a solution. It's not very elegant, but it seems to work, and it also looks like I can have multiple of these running in parallel, which is what I needed. It's a little bit frustrating that we have to go through all this, thinking about how easy it would be to integrate in Labview... Anyway, here's my solution for future reference. If you have any suggestions for improvement, please let me know!

0 Kudos
Message 5 of 8
(5,876 Views)

It turns out that while almost all of this works well and in parallel, the "To .NET Object" and ".NET Object To Variant" VIs seem to block execution for other calls of either VI, making this attempt fail.

Why do these VIs block execution? Is there another way to do this that does not block?

 

Thanks,

Ulf

0 Kudos
Message 6 of 8
(5,872 Views)

How's your project going?

 

I got a workstation with 44 core, and I'm facing the same problem like yours, calling multiple Mathscript will only use one matlab instance. I have to live in single thread world and leave 40 cores doing nothing around.

0 Kudos
Message 7 of 8
(4,832 Views)

Hi jiangliang,

 

That's an interesting problem. I would recommend posting a new question on the forum, as this thread is five years old. That should give you better visibility and a higher likelihood of a response.  

 

Best of luck on your project!

0 Kudos
Message 8 of 8
(4,812 Views)