LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Questions on Examples Mathscript Riemann Zeta and Shared library

Hello,

just uppgraded to Labview 8.6 and particularly amazed about Mathscript. Finally Labview has it all !

I am learning from two examples provided with Labview. The Mathscript using Riemann Zeta.vi and the Mathscript using shared libraries. Since i have some m fonctions i'll start with the first example.

In the Riemann Zeta Mathscript node, we can read the following:

 

a = cputime;
z = zeta(0.5+(k)*(35/n)*i);
b = cputime;
c = b-a

 

The function zeta is saved in a .m file as a normal matlab function and is located in the same folder as the vi.  When i tried to reproduce the same (write a code in the Mathscript node that calls a .m function saved in the same folder) LabVIEW is not recognizing the .m function. A red cross is appearing at the start of the line code that calls the function.

Any reason or way to fix that?

 

 

In the second example, Mathscript using shared libraries, the node loads a library "mydemo.dll" and "demo.h". My question in not directly on Labview here but on the shared libraries since I have limited information on them. Are they activeX dll ? Are they written in C++ ? Is there a way to compile my m functions in a dll ? what is the recommended language to create shared dll libraries? Any external tutorial on shared libraries is very much appreciated. 

 

Thank you very much

Sam 

0 Kudos
Message 1 of 10
(8,637 Views)

Please find attached the file codes related to the meantioned examples

Sam 

0 Kudos
Message 2 of 10
(8,636 Views)
Hello Sam,

Just as a curiosity, we are always looking to improve our help documentation.  Did you try searching for help on calling user-defined functions?  If not, that's okay; but if you did, did you find the documentation unclear?  Basically, in order to use an external .m file, you need to tell LabVIEW where to look for the files.  This is done through the search path.  There are three methods to set the search path for MathScript.  First, from a LabVIEW VI, go to Tools >> Options and click on the "MathScript: Search Paths" category item.  This will set the search path for all VIs in the main application instance only.  Second, from the MathScript Window, go to "File >> LabVIEW MathScript Properties" and click on the "MathScript: Search Paths" category item.  This will set the search path for the MathScript Window only.  Third, from within the project explorer, right-click on "My Computer" and select Properties.  Then click on the "MathScript: Search Paths" category item.  This will set the search path for that context only.

You can specify multiple directories to build up a search path for all your .m files.  The reason the Riemann Zeta example seemed to find the .m file next to the example is that when we created the example, we set the search path to look next to the VI.  When a VI is saved, it remembers the relative location of all .m files used.  Thus, you can copy the VI and the .m files to another machine and open it without a problem as long as the .m files stay in the same relative location.

As for the shared library example, I believe the "mydemo.dll" example DLL was written in C++.  I don't believe it uses ActiveX, but some of our tools for processing the header files with MathScript currently work on Windows only.  There isn't really a way to compile your .m functions into a DLL, but what you can do is create a collection of VIs that call your .m files and then build the VIs into a DLL using the LabVIEW Application Builder.  If you search the LabVIEW help index for "Shared libraries," there is a topic on "building (how to)" that will explain how to build a DLL.  If you are only interested in calling a DLL, you can read the topic on "Calling from LabVIEW."  You can also go to http://support.ni.com and browse the Knowledge Base and Tutorials.

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

Hello Grant

your reply is very helpful. I did not explicitly search for calling user-defined functions, but i did read many tutorials on Mathsripts (window and node) and i don't remember i read anything about search path. Then i started learning from the examples. 

While testing my functions in the Mathscript node i noticed two facts:

a variable cannot be of the same name as a function. (for example: `GetText=GetText (argument)` is not allowed)

the function should not finish with "end"

Hope it helps !

Cheers

0 Kudos
Message 4 of 10
(8,606 Views)
Hello Sam,

Thanks for the feedback.  We know about both issues.  Do you have a good use case for the first problem?  We have not fixed it because we couldn't think of a good use of such a construct and don't feel that it's very good programming practice to mix the use of variables and functions with the same name.  But if you have a good reason for doing so, we could raise the priority of the fix.

As for using "end" with functions, this is optional in MathScript.  It just so turns out that actually using it causes an error, but we hope to fix that in a future version.  Even if you are using local functions within a .m file, you do not need to use the "end" keyword to end a function.  In MathScript, one function ends where the next "function" keyword is detected.

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

Hello Grant,

I don't have any reason to use same variables as function names and i agree that it's not a good practice. I just wanted to point out some of the differences between the Matlab syntax and MathScript, that i have been discovering by trial and error.  

 

Now what i want to make sure of is the fact that all outputs of a Mathscript node are updated at the end of the exectution of the code inside the node. For instance, if A and B are inputs, C an output

 

C=A+B;

pause(5);

C=A-B; 

 

In labview C will have the value A-B. The update of C is done once at the end of the code and not sequencially. I guess i'll have to create a sequence struture in Labview and repeat the Mathscrip code. 

Thank you

Sam 

0 Kudos
Message 6 of 10
(8,593 Views)

Hello,

Here is an example about calling external functions with Mathscript node:

Calling a User-Defined MathScript Function Using the MathScript Node in LabVIEW

 

http://zone.ni.com/devzone/cda/epd/p/id/4788

 

The path issue is explained in the block diagram 

0 Kudos
Message 7 of 10
(8,577 Views)
Hello Sam,

The MathScript node functions like any other node in LabVIEW; that is, its outputs are only available once the node has executed in its entirety.  The output variable C in your example will have the value A-B.

As for your feedback on the Riemann Zeta example, I have talked about this with some other colleagues and we feel that the example might be slightly misleading.  We have saved it in a good state so that you can load and run it without any issues.  However, this perhaps conveys the message the .m files located next to a VI will be found automatically.  We are thinking of updating the example for a future release to have the user go through the task of adding a path to the MathScript search path in order to run the VI.

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

Hi Grant,

Regarding Mathscript documentation, I would like to ask how to find help about which algorithm is used for the left matrix division operator.

 

I'd like to know what does Mathscript do when the matrix is rank deficient?

 

Thanks

0 Kudos
Message 9 of 10
(7,834 Views)

Hi Joshuak,

 

MathScript returns LS solution when matrix is singular.

Message 10 of 10
(7,828 Views)