LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with Mathscript node

Hi,
 
Apologies if anyone is reading this for a second time - I posted on the main Labview forum before I was aware the Mathscript forum existed.
 
I wonder if anyone can explain to me why if I program inputs directly into a Mathscript node (e.g. a=1) the algorithm will run fine and output the results to a numeric indicator, but if I wire the inputs from a numeric control to the Mathscript node the algorithm outputs are incorrect (0).
 
I need to program it the first way as the input values will change depending on user input. I have attached a VI to illustrate my problem.
 
Many thanks to anyone who can solve this (probably very simple!) problem for me
 
Cheers,
 
Ian
 
P.S. I am running Labview 7.1 and Matlab 7.1
0 Kudos
Message 1 of 4
(7,154 Views)
Hello Ian,

Unfortunately, the MathScript Node was not available in LabVIEW 7.1 (It was introduced in LabVIEW 8.0).  I think what you mean is the MATLAB® script node.  However, I do not see the problem you describe.  I can open the VI, set the a and b controls to 1 and 4, respectively, and run the VI.  The c and d outputs produce the same results as g and h.  The problem you see may result from the default values of controls.

You can set the value of a and b to be whatever you want.  Running the VI will produce the correct results.  However, if you close the VI and reopen it, the controls will lose their values -- they will be set to 0.  Running the VI now will produce 0 for the outputs.  In order to set default values, change the values of a and b to be the correct values.  Then select Edit >> Make Current Values Default.  Now save the VI and when you reopen it, it will run correctly.  If you use this VI as a subVI and always wire the a and b inputs, then the default values will not matter as the values on the wires will always be used.  If you are using the VI in this latter case, can you also post the caller VI?

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

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 2 of 4
(7,146 Views)
Apologies - you are right when you say I am using the MATLAB® script node.
 
And also a big thank you for solving my simplistic a+b problem Smiley Happy
 
I wonder if you could work your magic again with the actual problem I am trying to solve: converting the following code into a Labview program.
% routine for calculating Transfer Function
% signal is the array containing the measured signal
% sigs is the array containing the input sine wave
% sigc is the array containing the input cosine wave
freq=11.45;
sfreq=52.4*freq;
cycs=10;
t=0:1/sfreq:cycs/freq;
n=cycs*52.4+1;
ph=60;
signal=0.2*sin(2*pi*freq*t+ph*pi/180);
sigs=sin(2*pi*freq*t);
sigc=cos(2*pi*freq*t);
real=signal.*sigs;
comp=signal.*sigc;
a=(2*sum(real)-real(1)-real(n))/(n-1);
b=(2*sum(comp)-comp(1)-comp(n))/(n-1);
R=sqrt(a*a+b*b)
ang=atan(b/a)*180/pi
The outputs that should be displayed (and are displayed when the program is run directly in MATLAB, leading me to believe there are no errors in the code itself) are that R = the amplitude of the signal ('amp' in my attached VI) and ang = the phase of the signal (ph my attached VI).
 
I will eventually be using this custom filter as a sub VI in a program to analyse tyre properties, where 'signal' will be a real life signal generated from data acquisition from the test rig.
 
Cheers again for your help,
 
Ian
0 Kudos
Message 3 of 4
(7,141 Views)
Hello Ian,

As you say, the script you posted works fine when run directly.  However, the VI you posted has some errors.  Unfortunately, there seems to be a bug in the MATLAB® script node so it does not return the errors reported in the script -- it only mentions the fact that the R variable does not exist.  The problem is the generation of the t variable.  Subsequent usage of t then generates an error and R is never calculated.  The variable t is calculated with a step size of 1/sfreq.  However, you are already computing the reciprocal of freq*samples per cycle and passing this value into the script node as sfreq.  Simply remove the reciprocal primitive from your VI and it should work fine.

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

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
0 Kudos
Message 4 of 4
(7,123 Views)