11-20-2009 03:39 AM
Hi,
I am trying to use a matlab function in labview. This function works fine in matlab itself. I made sure the Mathscript Search Path is correct. However, I cannot execute the function. When I run the function in the Mathscript Window the output window gives: "unexpected token: function". I can't use the function in a script either. The function wil not be recognised.
I have already made a very basic function to multiply a given number with a constant, just so see how things go. With that function I get the same line in the output window(unexpected token: function), but I can execute it in labview.(the code for it is just: function s = som(dt) s=5*dt)
What could be the problem?
I attached the script and function I want to use.
Thanks
11-20-2009 10:16 AM - edited 11-20-2009 10:18 AM
Hello diets,
This error can occur when you type the 'function' keyword when you're trying to call a function. This keyword needs to be at the top of the .m file but it should not be used when you are calling the .m file.
For example, for your sample function, the file som.m would contain this:
function s = som(dt) s=5*dt
And then you would call it in the MathScript Window or MathScript Node like this:
x = som(3);
Please give this a try. If it is still not working, let me know and we can try some other ideas.
Good luck!
jattas
LabVIEW MathScript R&D
11-24-2009 06:22 AM
I am still having some difficulties.
I can call the basic function (x=som(3)) in the mathscript node. In the mathscript window, using the command window, I get the following line: unknown symbol on line 1: som. So in the mathscript window the function doesn't work.
What I eventually would like to make is a function that uses mergesorting to sort a multidimensional matrix. Right now I have a function to sort just a vector. This function called 'mergeSorting' works fine in Matlab. When I use it in a mathscript node I get the following error: user defined function contains an error. In the mathscript window the same line as for the basic function 'som' appears: unknown symbol on line 1: mergeSort. Any ideas?
I attached a txt.-file with the code for the function mergeSorting(x) (with x a row vector).
11-24-2009 04:34 PM
Hi diets,
I think that a few things may be going wrong. Two of them should be easy to fix but one of them is more fundamental.
First, the search path that the MathScript Window uses to find .m files is different from the one used by the MathScript Node. To set the MathScript Window search path, go to File>>LabVIEW MathScript Properties in the MathScript Window. If you change this path to include the location of your som.m file, I'm guessing that you'll be able to call it.
Second, when calling a function in a .m file the top-level function name and the .m file name must match exactly. If you want to call 'mergeSort', it must be saved in 'mergeSort.m'. It sounds like you may be mixing 'mergeSort' and 'mergeSorting'. This could explain the "unknown symbol on line 1: mergeSort" error.
Lastly, MathScript doesn't currently support recursive function calls. Unfortunately the Merge Sort algorithm is inherently recursive. When I tried calling your attached script in the MathScript Node, the VI broke with the error "Recursive function call detected". The best workarounds we can offer at the moment are either to restructure your algorithm to be non-recursive, to implement it using LabVIEW recursion,or to call the built-in MathScript sort function instead. Which workaround is best for you depends on exactly what you're trying to achieve with your application.
Good luck!
jattas
LabVIEW MathScript R&D