LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Can I call a User-Defined Mathscript Function using LabVIEW 7.1?

I am trying to use a User-Defined function call in a mathscript node using LabVIEW 7.1 and I cannot seem to make it work.
 
For example, consider that my mathscript node contains:
 
clear all;
close all;
x = linspace(1,100,5);
y = mltp(x);
 
where the labview vi is saved in the same folder as the mathlab script mltp.m that is given by:
 
function out1 = mltp(in1)
out1 = 5*in1;
 
Creating an array output of the mathscript node for y yields an empty array.
 
How can I use User-Defined functions in a mathscript node using LabVIEW 7.1?
 
 
0 Kudos
Message 1 of 9
(3,258 Views)
Did you properly set your path?

From the Help for saving scripts:
You must save the function or script in a directory that you specified in the Path section of the MathScript Preferences dialog box.
From the Mathscript Preferences Dialog Box Help:
  • Path—Sets the default search path list. LabVIEW searches the path list from top to bottom to find functions or scripts that you defined and want to execute in the LabVIEW MathScript Window. Use the MathScript path function to modify the search path list for the current instance of LabVIEW. LabVIEW resets the search path list to the default when you restart LabVIEW.

0 Kudos
Message 2 of 9
(3,252 Views)
Hello,

The MathScript node was not introduced until LabVIEW 8.0.  Are you perhaps using the MATLAB script node?  Unfortunately, there is not a search path section of the Options dialog like there is for the MathScript node.  You will need to set the path explicitly.  Instead of hard coding a path, take a look at the Lorenz equation shipping example to see how to set the path if you wish to locate the user-defined function .m file next to your VI: <LabVIEW>\examples\scriptnode\Differential Equation.llb\MATLAB Script - Lorenz Diff Eq.vi.  Instead of using the "Default Directory" path primitive, you will want to use the "Current VI's Path" primitive from the File I/O >> File Constants palette.  Then use the Strip Path primitive, convert it to a string, and pass the path to the MATLAB script node.

MATLAB® is a registered trademark of The MathWorks, Inc.

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 3 of 9
(3,244 Views)
Works like a charm.
 
Thanks!
0 Kudos
Message 4 of 9
(3,232 Views)

I've got basically the same problem here (so no point in starting a new thread). I'm running Labview 8.2 btw...

 

The above solution (which I've seen in several places on the NI site) is not working for me. When I hit tools>options there is no mention of mathscript. I tried opening the mathscript window and adding the location of my user-defined m-file to the search list but that didn't work. Tried moving the m-file to the default "Labview data" folder and that didn't work. I added the path function to the node to point to where the m-file is and that didn't work either.

 

For the sake of trying to get the thing to work I defined an m-file to simply mutiply two numbers together when called from the mathscript node to avoid any problems that may occur in the actual function I want to use but this thing just does not want to run it.

 

Any suggestions?

 

Thanks.

0 Kudos
Message 5 of 9
(3,075 Views)
Hello,

LabVIEW 8.2 did not yet have the MathScript page in Tools >> Options.  In this version, you need to set the search path through the MathScript Window.  Launch the window from Tools >> MathScript Window.  Then go to File >> MathScript Preferences.  It sounds like you have done this.

A first guess is that your .m file calls some unsupported functions or uses unsupported syntax.  If that were the case, you could put the script in the Script Editor of the MathScript Window and select the "Save and Compile Script" menu item.  This will return any errors from compiling the script.  However, you said you simplified the problem to a single multiplication.  Since this still does not work, I need more information.  What are all the directories listed for the path in the MathScript Preferences dialog from the MathScript window?  What is the working directory?  Where is your .m file located on disk?  Can you attach your .m file or paste the contents in your message?

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

Path:

C:\Documents and Settings\control\My Documents\LabVIEW Data

C:\Program Files\MATLAB71\TOOLBOX_calib                                               <- This is where my m-file is.

 

Working Directory:

C:\Documents and Settings\control\My Documents\LabVIEW Data

 

The script in my node is:

out = xxx(B, C);

 

And the contents of xxx.m:

function [A] = xxx(B, C),
A = B * C;

0 Kudos
Message 7 of 9
(3,053 Views)
Hello,

It looks like the problem is that MathScript does not support a comma after the function definition.  Replace the contents of your xxx.m to be
function [A] = xxx(B, C)
A = B * C;

Debugging capabilities weren't very good in the first versions of MathScript.  To determine the problem, I wrote the xxx.m file in the MathScript Window's Script editor.  Then I used the File >> "Save and Compile Script" menu item.  It reported a syntax error at the end of line 1, which was the comma.  I removed that and the script compiled fine.  In LabVIEW 8.2, this is the only method to see errors reported from .m files.  I will investigate using a comma at this location to possibly fix it in a future version.

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

Hi,

 

That works for me too. I'm actually upgrading right now so hopefully that'll help with debugging my original m-files.

 

 

Thanks for the help all.
0 Kudos
Message 9 of 9
(2,996 Views)