04-16-2016 03:07 PM
I want to use function handle in mathscript, in matlab the only two ways are to use @ or str2func, both of these methods are not supported in mathscript.
is there a way to use function handle in mathscript?
04-16-2016 03:53 PM
Why? What are you trying to compute? Might it not be faster and easier to simply "do it in LabVIEW"?
Bob Schor
04-16-2016 03:58 PM
i'm using a function from a toolbox that uses function handle in it.
04-16-2016 04:17 PM
OK. Can you program "a function from a toolbox" in LabVIEW? Or is it a secret?
Yes, you can (to a limited extent) mix LabVIEW and Matlab. As you have already discovered, it is not always so easy to do so. I'm sorry my original question wasn't sufficiently clear, so I'll rephrase -- please tell us precisely what it is you are trying to compute, so we can suggest if there is a way to do it entirely in LabVIEW, as that will be (a) faster to run (since LabVIEW is compiled and Matlab is interpreted), (b) simpler to maintain (as it involves only LabVIEW, not LabVIEW + Matlab), and (c) simpler to document.
Bob Schor
10-05-2016 09:30 AM
Hello
I have a similar problem. The str2func and inline function do not work in my mathscript (Error -90031 occurred at Unknown symbol on line 10 column 3: inline/str2func).
Situation sketch: I read a function from a txt file as a string and I want it to become a waveform. E.g. u=sin(5*t); or u = idinput(400,'sine',[5/(100*pi), 50/(100*pi)],[-1 1],[28,10,1]);. The formula can be written in specific Matlab language, so Mathscript is preferred. Or is there another solution?
Mathlab code:
function [data,fctStr] = GenerateSignal( stg ,TimeInterval, Period )
%GenerateSignal Generate an array of datapoints of a function
% Output: Array of doubles
% Input: string with function e.g. 'u=@(t)sin(5*t);'
% TimeInterval: space between two samples
% Period: time of one cycle
equal_indices = strfind(stg,'=');
fctStr=stg(equal_indices(1)+1:end-1); %fctStr='@(t)sin(5*t)'
f=str2func(fctStr); %f=@(t)sin(5*t)
i=0:TimeInterval:Period;
data=f(i);
data=data.';
Thank you in advance.
Jasper
10-06-2016 02:54 AM
Hello
I have the similar problem too!I don't know how to use handle function in mathscript.If you have a solution please let me know,my e-mail address kterl@sina.com.
Thank you!
10-06-2016 02:57 AM
Hello
I have the similar problem.Is there any way to solve this problem,please tell me.Thank you very much.
10-06-2016 10:30 AM
If the functions you are looking for are not in the mathscript node, you can look at this: https://zone.ni.com/reference/en-XX/help/371361M-01/lvhowto/creating_a_matlab_script/
The question may be why do you need to pull a function from a text file? Is there a finite number of functions or function types that can be programmed into LabVIEW and then called with the correct parameters instead of trying to dynamically create the function?
03-01-2017 11:53 AM - edited 03-01-2017 11:55 AM
Hi,
I believe that there is a solution, but it is not very pretty. I also just want note that the reason that I am very interested in this topic is because I am trying to do almost the same thing as you. So if anyone else finds an alternative to the solution that I am about to post then I will be forever in your debt.
Essentially the only way that I have found to do this is to use the "eval" function. I know that this isn't a very computationally inefficient way of doing it, but "eval" is a very powerful tool. I am also surprised that this function is supported in LV while "str2func" and "inline" are not.
The reason that I am interested in this is that it gives the user the ability to flexibly process data (in my case hyperspectral image data) in a very easy way that would be very difficult to do without rewriting the entire LV code.
Having said that though the "eval" function slows the processing down to snails pace. This is to be expected as it is recompiling the code each iteration. So if anyone does have a more elegant solution then I would massively in your debt (well....I would buy you a pint at the least!).