LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

calling a simple mathscript function from a mathscript node


I'm trying to call a simple mathscript function from a node in LabVIEW 8.5. But the vi doesn't recognize the function even though the function is in the same folder as the vi and the folder is added to the mathscript path. Note I created the m file using the Mathscript window.

Here is the function.

 % average function
function z = ave(a)
% This computes the average value

s = size(a);
sm = 0;
for n = 1:s
  sm = sm + a(n);
end
z = sm / s;

 And in the vi's mathscript node is the following

 %average test
av = ave(A);

I supply a 1D array to it.  It errors at the function call (2nd line).
 

I can't understand why it fails. Remember that both the vi and m files are in the same folder. The m file is named "ave.m".

Also - I read that having a script longer than 20lines can slow the performance. Assuming I get past this silly problem and use user defined functions, is the performance any better if I call functions to reduce the number of lines of code?

Please help
 
0 Kudos
Message 1 of 5
(2,976 Views)

The error you are seeing is probably coming from the last line of ave.m.  In this function 's=size(a)' returns a 2 element vector.  Dividing 'sm', a scalar, by 's', a vector will cause an error.  If you use 's=length(a)', then 's' will be a scalar and the error will go away.

 

Chris M. 

0 Kudos
Message 2 of 5
(2,967 Views)
Thanks for catching that error, Chris.  I still get the error even after using 'length'. The error is 'Unknown symbol on line 1: ave'.
0 Kudos
Message 3 of 5
(2,949 Views)
quick search about this error see here and here
0 Kudos
Message 4 of 5
(2,943 Views)
Hello,

Since you mention you are callling the function from the MathScript node, make sure you have set the search path to find your function.  By default, LabVIEW MathScript will only search the "My Documents\LabVIEW Data" directory.  If you wish it to find the .m file you have placed next to your VI, go to Tools >> Options.  Select "MathScript: Seach Paths" and add the directory to the list.

If you are working in a project, the MathScript search path setting is a per-project setting.  In your project,  right-click on the "My Computer" target and select Properties.  Click on "MathScript: Search Paths" and add your path there.

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