From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

matlab calling labview via activex, passing parameters

I've been using Matlab and LabVIEW for well over a decade.  Never tried to get them to talk to each other before.  As far as I can tell, it ought to be simple.

From the LV end, just turn on the VI server.
Seems simple enough from the Matlab end:
e=actxserver('LabVIEW.Application');
vipath='c:\Documents and Settings\mwolfson\Desktop\testx.vi';
vi=invoke(e,'GetVIReference',vipath);

I can run the VI with:
vi.Run or vi.Call or vi.Call2

The trouble is I have no idea how to pass parameters to the VI and how to read back the output. I've munged together that I can set the input arguments what seems like the hard way:
vi.SetControlValue('input variable1',4);

But if I try to use the NI suggested method "Call" or "Call2", Matlab chokes on any input parameter I specify.  Matlab lists the "syntax" as:
object.Call2([paramNames], [paramVals], [openFP], [closeFPAfterCall], [suspendOnCall]), but when I try to use this, it chokes.

>> vi.Call2('input variable1',5)
??? Invoke Error, Dispatch Exception: The parameter is incorrect.



Any ideas?  And why is such a thing so painfully non-obvious (or described somewhere)?

TIA,
  -- MW
0 Kudos
Message 1 of 11
(6,212 Views)
Well, google turned up exactly one search result and it works.  You guys ought to have it documented better than one forum post with an inaccurate subject line.

e=actxserver('LabVIEW.Application');
vipath='c:\Documents and Settings\mwolfson.REDSHIFT\Desktop\testx.vi';
vi=invoke(e,'GetVIReference',vipath);
vi.SetControlValue('in',4)
vi.Run
vi.GetControlValue('out')

The tricky part is that Matlab didn't list GetControlValue as an invoke method.

  -- MW

0 Kudos
Message 2 of 11
(6,206 Views)

Hello,

 

Sorry for digging out this old thread, but it's most relevant to what I wanted to do that I could find.

 

I have a compiled .exe LabView program which has a slider GUI in it. I want to write some MATLAB code to do some calculation and eventually set the slider value, instead of human doing it.

 

The ActiveX examples I found are all writing data to Excel from LabView. Your project seems more similar to what I wanted to achieve here, especially with the line vi.SetControlValue('in',4).

 

My question is:

1) Would it work?

2) Is ActiveX the best choice?

3) Any simple example to start?

 

Thanks!

0 Kudos
Message 3 of 11
(4,021 Views)

1) Maybe but it's almost certainly more complicated than you think

2) Almost never if there's an alternative

3) Yes:

 

LabVIEW actually has a node that lets you run MATLAB script directly.  Here is a link to the help page on it, which points to two examples that ship with LabVIEW if you use the "Help-->Find Examples..." menu selection.

0 Kudos
Message 4 of 11
(4,018 Views)

Hi Kyle,

 

The MATLAB program is actually complicated and uses external Java jars, so running it inside LabView may be tricky.

 

My plan is run the MATLAB program and the LabView program in individual .exe, and expose the slider from LabView on ActiveX so MATLAB can see and change it. I just couldn't find an example except the original post from this thread doing that.

0 Kudos
Message 5 of 11
(4,013 Views)

Well, you have to activate the ActiveX server first if you haven't already. 

 

I couldn't find MATLAB examples of using ActiveX to call LabVIEW, but this project using C code and this project with visual basic have some examples and you might be able to figure it out from that.

0 Kudos
Message 6 of 11
(4,008 Views)

I feel like I am almost there. After activating the ActiveX server in a .vi, I can have MATLAB run the .vi with the following commands:

 

e = actxserver('LabVIEW.Application');
vipath = 'F:\Joe\LabView\test\called_by_matlab.vi';
vi=invoke(e, 'GetVIREference', vipath);
vi.Run

"vi.Run" will launch the LabView editor. Even thought the editor does not show any .vi, but the voltage outputs changed, so the .vi must be running in the background.

 

The problem is after "vi.Run", the command prompt in MATLAB just freezes, waiting for the .vi to finish. If I close the LabView editor manually, I gain the command prompt back.

 

I can use 

vi.GetControlValue()
vi.SetControlValue()

to get and set the slider value, but only before "vi.Run". What I wanted to do is change the slider value during "vi.Run". Is there any way to run "vi.Run" asynchronously?

 

 

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

Have your VI with the slider already running, and have the VI you call with VI.run call a different VI, and that one just sends a message of some kind (queue, user event, notifier, updating a FGV, opening the VI reference and finding the control reference from the front panel, etc.) that the other VI already running will get and uses that to trigger the slider to move.

0 Kudos
Message 8 of 11
(3,974 Views)

Finally figured it out. I just have to launch the LabVIEW executable separately, and hook up to it in MATLAB.

 

  1. Activate ActiveX server in LabView.
  2. Build .exe from the .vi. In Advanced section, check "Enable ActiveX Server" and give it a name.
  3. Copy the compiled .exe and .tlb, with the .vi, to the deployment machine.
  4. Switch to MATLAB, and use "!program.exe &" command to launch the compiled LabVIEW program.
  5. e = actxserver('XXX.Application');
    vipath = 'F:\Joe\LabView\test\called_by_matlab.vi';
    vi=invoke(e, 'GetVIREference', vipath);
    Use the above commands in MATLAB to "hook up" to the running LabVIEW executable. "XXX" is the name set during building the executable in LabVIEW, and vipath is .vi that got copied over.
  6. To change a component's value, use vi.SetControlValue('label', 0). 'label' is the label string of the component in the .vi.
0 Kudos
Message 9 of 11
(3,961 Views)

If you want to launch an instance of LabVIEW executable via ActiveX from MATLAB, use the following code:

One requirement is LabVIEW has to be installed on the same machine.

 

e = actxserver('LabVIEW.Application');
vipath = 'F:\Joe\LabView\test\called_by_matlab.vi';
vi=invoke(e, 'GetVIREference', vipath);
vi.Run(true);

vi.Run(true) tells the function to run asynchronously.

Here is the document for all the APIs http://zone.ni.com/reference/en-XX/help/371361P-01/axprop/vi_class_method/

0 Kudos
Message 10 of 11
(3,950 Views)