LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

LabView 8.20 MathScript Dynamic Loading

Does anyone know if LabView 8.20 MathScript supports dynamically loading m-files? I would like to be able to be prompted for a path to an m-file, choose the m-file and have LabView compile and run the script.

For the sake of this question, lets say that I am adhering to some "function prototype", assuring that my loaded m-file has input and output variables that match those set up on my MathScript node.
0 Kudos
Message 1 of 10
(9,655 Views)
Hello,

The appropriate solution depends on your particular setup.  Can you provide any additional details about what you are trying to accomplish by dynamically calling .m files?  In particular, do you know the set of available functions you wish to call and simply want to choose among them?  Or are you more interested in a general purpose VI where .m files could be added at any time in the future and updating the VI is impractical?

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 2 of 10
(9,647 Views)
Hi,
My ideal situation would be a general purpose VI that could load any m-file. This would be part of a larger LabView software project so the user wouldn't have access to block diagrams, etc.
The basic idea is that the user doesn't have the ability to copy their script into the MathScript node and they don't have the ability to create inputs and outputs on the MathScript node.
I don't know the functions that will be used in advance. I would need it to work with basically any m-file. In reality I would probably need to define some standard interface to get it to work.
The first step is to get to a point where the user will run a VI that will bring up a file dialog. They will choose their m-file from the dialog and the script will be placed in the MathScript node. Is this possible?

Thanks for your help!
0 Kudos
Message 3 of 10
(9,653 Views)
Your particular use case is very interesting as we have been thinking a lot about it lately.  There is no really easy way to do what you wish.  Could you describe your application a bit more?  Perhaps we can discover a better solution.

You mention that you won't know the functions that will be used in advance.  Unfortunately, this will have some performance implications because any new script will need to be compiled before it can execute.  If you did know the functions you were going to call, you could use some if or switch logic to implement a selector to choose among the different m-files.

It isn't possible to place the script into a MathScript node when the VI is running.  Instead, what you'll want to do is bring up the file dialog for the user to choose a file.  You can then use LabVIEW's Strip Path primitive to separate the path from the m-filename.  You will also want to remove the .m extension from the filename so you just have the function name.  You can pass the path and the function name into a MathScript Node that has all the other inputs and outputs configured per the common prototype.  Simply call the 'path' function in MathScript to set the value you passed in to be the current path.  Then, build a string by concatenating the output variables with the function name and the input parameters, e.g.
str = ['a = ' func_name '(x, y, z)']

With the path set, MathScript will be able to locate the function you are calling.  To execute the function, use the 'eval' command.  The 'eval' command also takes a slight performance hit in order to evaluate the string.  If you also wanted to require that all functions have the same name (so that they are only differentiated by their location on the filesystem), you could improve performance slightly.  By naming all the functions 'fname,' you could replace the string concatenation and eval call with a simple call to the function, .e.g.
a = fname(x, y, z)
and MathScript would use the function found in the current path.

Please let me know if you have further questions.

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
0 Kudos
Message 4 of 10
(9,614 Views)
I tried the method that you outlined and it seems like it will work for me. You answered the question that I was asking. I still don't have LV 8.20 so I don't have access to eval yet. However I am confident that this workaround will do the trick. Thanks for your help.
0 Kudos
Message 5 of 10
(9,606 Views)

This solution is fine for design time.

However I need to implement similar functionality within .exe application.

Is there any solution? It seems to me that MathScript newer was ment to be used in executable. So for all my dynamic calculations I still need Matlab which is major overkill for what I am looking for.

 

Thank you

Vlad

0 Kudos
Message 6 of 10
(9,464 Views)
Hello Vlad,

Starting in LabVIEW 8.20, the MathScript node can be used in a built application.  However, due to the nature of how MathScript works, certain functions are unavailable in the run-time engine.  For a list of these functions, see the help topic titled "MathScript Functions Not Supported in the LabVIEW Run-Time Engine."  

You specifically mention the ability to perform dynamic calculations.  If you simply need the user to be able to choose among several different algorithms for a particular calculation, this can be done in an executable.  You can present an algorithm selection to the user on the VI's front panel and feed a particular value into a MathScript node.  You can use a switch statement in the MathScript node to call a particular algorithm.  As long as all the functions you intend to call are called (even conditionally) in the MathScript node, you can dynamically select which one to call in the executable.  When building the executable, you must remember to include all the .m files for the user-defined functions you are calling.

However, if you need the user to be able to call a user-defined function that was not known when the executable was built, this is not currently possible in MathScript.  Depending on the complexity of what you are trying to do, there may be a non-MathScript solution.  We would like to learn more about such an application in an effort to support this behavior.  Can you provide an example of what you are trying to do?

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
0 Kudos
Message 7 of 10
(9,452 Views)

Thanks Grant!

I do not know the function ahead of time.

Basically what i am looking for is to create user programmable calculator as a part of much larger application.

It has 8 (X1...X8) inputs and 1 ( Y) output. The values for inputs come from other parts of application. User can type formula in place holder and vi performs calculations based on it. (for example Y=X1+((x2-X3)/2) ). The type of formula can involve any kind of math functions.

Currently I have implemented it using formula parser VIs but I need polymorphic functionality to be able to work with Complex numbers and also arrays of real or complex numbers.

I think my best bet would be if EVAL function was properly implemented in RunTime but sadly it is not. I already contacted support and they confirmed the bug so I was looking for workaround.

Vlad

0 Kudos
Message 8 of 10
(9,446 Views)
Hello Vlad,

You were one step ahead of me.  The formula parsing VIs can provide dynamic calculation in the LabVIEW application builder.  But, as you have discovered, they do not support complex numbers or arrays.

MathScript is built on top of LabVIEW and consequently uses many pieces of LabVIEW during its code generation.  Not all parts of LabVIEW are available in the run-time engine.  Functions that require MathScript to generate code (such as "eval") currently will not work with the run-time engine.  Unfortunately, there is no workaround at this time, but we will consider adding support in the future.

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
0 Kudos
Message 9 of 10
(9,431 Views)

Thanks Grant!

I am looking forvard to it.

In mean time I found the following work around:

All of our test stations equiped with Agilent Vee Runtime.

I created function in Vee which can do arbitrary math by interpreting formula on a fly( One of the nicest features of VEE).

Then I wrapped ActiveX call to VEE into Lv vi.

This let me achieve desired functionality. I took perfomance hit since VEE is out of process server but I don't see any other option at this time.

Thanks again for your help

Vlad

 

 

0 Kudos
Message 10 of 10
(9,428 Views)