LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamically import .m file into Matlab Script

Solved!
Go to solution

I would like to dynamically load a Matlab .m file into the Matlab Script function in a VI.

In other words, based upon a path to a .m file (control on the FP), load the .m file into the Matlab Script and then execute it.

Does anyone know how to do this?

0 Kudos
Message 1 of 10
(6,046 Views)
Solution
Accepted by RedSoxFanatic

Hi RedSoxFanatic,

 

It's not possible to do what you're asking.  However, I can think of a couple options that will give you the same behavior.

 

1.  If you only have a few of your .m files that will be called you can use a case structure with these files already loaded.  The user can then choose which case to run.

 

2.  You can have the Matlab Script Node run you particular .m file based on an input (number maybe), but this does also require you to have a predefined list of your .m files.

 

Justin D.

Justin D.

Applications Engineer
National Instruments
0 Kudos
Message 2 of 10
(6,027 Views)

Thanks for the suggestions.

A coworker suggested dynamically loading a VI from a specific location. With a simple edit to the VI (new .m file) an application could dynamically load the VI and run the new .m file.

0 Kudos
Message 3 of 10
(6,022 Views)

Hi RedSoxFanatic

 

I have the exact need. I wonder if you can post your example VI code on  dynamically loading the VI and running the new .m file

 

many thaks in advance

 

xg

 

0 Kudos
Message 4 of 10
(6,001 Views)

I have not yet written the code.

However, there is not much to it. Use the "Open VI Reference" function that has a path to your VI with the Matlab Script in it. The VI will have inputs and outputs (must always be the same no matter what VI you dyamically load) that match the inputs and outputs to the Matlab Script. After the VI has run, close the VI reference.

0 Kudos
Message 5 of 10
(5,993 Views)

many thanks. I will have a go.

 

xg

0 Kudos
Message 6 of 10
(5,988 Views)

I would like to point out that the chosen solution by the National Instruments applications engineer is unfortunately not correct. There are at least two ways that this can be done.

 

  1. You can use a MATLAB script node and then use VI Scripting to interact with the node. For example, the following snippet will dynamically create a VI, add a MATLAB script node to the block diagram, populate the node with a script and output terminal, and then create an indicator. A similar method could be used to find an existing node and edit a script by editing the text and adding or removing inputs and outputs. Note that this has limitations in that it requires the development environment and thus cannot run in the run-time engine.
    Script MATLAB Script Node.png
  2. The second option is to use the MATLAB COM automation sever through ActiveX. Search the MATLAB site for more information regarding this. The below snippet is a way to dynamically execute an expression or script text. Note that evaluating a .m can be done similarly with the appropriate Feval method. This is actually the method that the MATLAB script node uses in the background. The method has the benefit of being able to run in the run-time engine but is a little les LabVIEW friendly in terms of the MATLAB script being more readable and easier to interface with other LabVIEW code.
    Execute MATLAB via ActiveX.png

Hopefully this helps anyone who comes across this page! If someone could please unmark the current solution as correct, that would be great as well.

Message 7 of 10
(5,321 Views)

What do I need to install to have MLApp.DIMLApp accessible? How to find this ActiveX Class in the browser?

Michał Bieńkowski
CLA, CTA

Someone devote his time to help solve your problem? Appreciate it and give kudos. Problem solved? Accept as a solution so that others can find it faster in the future.
Make a contribution to the development of TestStand - vote on TestStand Idea Exchange.
0 Kudos
Message 8 of 10
(3,851 Views)

I installed Matlab Runtime and I guess I found the proper file here:

C:\Program Files\MATLAB\MATLAB Runtime\v96\bin\win64\mlapp.tlb

I was unable to find it in the list populated in "Select Object From Type Library" window. I needed to browse and manually select the mentioned TLB file.

lib.png

I even registered it running:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\regtlibv12.exe "C:\Program Files\MATLAB\MATLAB Runtime\v96\bin\win64\mlapp.tlb"

But I'm still getting error 3005 (no matter which Object I select).

 

How to fix the error and why Matlab lib need to be manually selected and registered?

 

Michał Bieńkowski
CLA, CTA

Someone devote his time to help solve your problem? Appreciate it and give kudos. Problem solved? Accept as a solution so that others can find it faster in the future.
Make a contribution to the development of TestStand - vote on TestStand Idea Exchange.
0 Kudos
Message 9 of 10
(3,839 Views)

Hi bienieck,

 

This would be a good topic for a new forum post since the initial topic here has been answered.

 

Thanks!

Ramsey

Technical Support Engineering

National Instruments

0 Kudos
Message 10 of 10
(3,833 Views)