From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

MathScript within For loop

Hi,

 

I'm trying to perform the following program flow on a set of 1000 data samples:

 

(1) Take data values Q1, Q2, L1, L2, L3 from a spreadsheet file.

(2) Perform a MathScript calculation on the data values

(3) Output the result into an array (store the result in an array)

(4) Step on to next data values Q1, Q2, L1, L2, L3 in the data series and repeat (2) onwards

 

I have managed to read data from the spreadsheet file and can load the individual data values.  I am struggling to achieve a method of obtaining the results of each individual MathScript calculation and loading them into an array for viewing.  I have attached a vi with my attempt however the output array only appears to show just 3 results but I'm not sure if these are correct, in sequence, etc.

 

If anyone can provide some guidance or advice it would be greatly appreciated.

 

Regards,

0 Kudos
Message 1 of 13
(4,029 Views)

I think the problem is a confusion about arrays and how For loops handle them.  It looks to me like you first read the spreadsheet and create several identically-sized arrays of data you want to use in a computation.  Since you are using a MathScript to do the computations, you have a choice --

 

1) Give the script the Arrays, and let MatLab do all the work (since it can do arithmetic on arrays).  If this is the case, you don't need a loop at all, since you'll pass the arrays in (once) to MatLab, it will compute, and pass (array) answers out.

 

2) Give array elements one-at-a-time to the MatLab script and let it do computations on single elements.  Here is where the LabVIEW For loop shines -- if you wire the array in on the left side, by default it uses an "index" input (symbol looks like a box within a box) which pulls of one element each time through the loop.  When you bring the answer out the right edge of the For loop, by default it will also be "indexed", meaning it will automatically become an element of an array.  The best part about this is when you use arrays as inputs, you don't need to (and probably shouldn't) specify the "N" number-of-iterations for the For loop -- it will automatically loop through all of the (identically-sized) arrays for you.

0 Kudos
Message 2 of 13
(4,008 Views)

There are many really (really!) questionable constructs in your VI:

 

  • Why do you build your 2D arrays into a 3D arrays? (several occasions)
  • What's uo with all the shift registers? None are needed here

 

Can you attach a typical datafile?

 

 

I would recommend to start with a few LabVIEW tutorials.

0 Kudos
Message 3 of 13
(4,003 Views)

Why do you even need MathScript here at all??? Just use plain LabVIEW. That code in your MathScript node is not rocket science. 😄 

 

Here is a quick attempt at option (2) from Bob above. In addition, I removed most of the other nonsense from your code.

 

I don't know how your array is arranged, so you might need to transpose. Modify as needed.


Maybe it can point you in the right direction.

 

0 Kudos
Message 4 of 13
(4,000 Views)

Many thanks for your assistance and observations.  I am not an experienced user of LabVIEW hence my attempt may be viewed as somewhat strange.  I have studied various help files and examples however I do find that LabVIEW has numerous ways to achieve a solution.  You are right that the MathScript file (so far) is fairly simple however I'm going to develop this further once I have the principle of reading in, manipulating and outputting data (your example has given me something to look into for this, thanks).

 

I appreciate your help and assitance on this.

 

Regards,

0 Kudos
Message 5 of 13
(3,983 Views)

Before starting to use LabVIEW, I did a lot of data analysis, plotting, etc. using MatLab -- I even made some "GUI" applications.  However, now that I've seen the power of LabVIEW, particularly the way it handles "data flow", I've stopped using MatLab, only considering it if there is a particular Special Function or Powerful Analysis Routine that I need (and that LabVIEW doesn't have readily available).  So far, those instances have been rare.

 

Note that the "guts" (sorry for the crude language) of your MathScript node seem to be fairly simple arithmetic calculations on single array elements.  You should be able to cobble together this calculation by using a few functions from the Numeric menu.  Note if you need to add or subtract more than two terms, you can use the Compound Arithmetic node (click on it and then click the Help button to see how to use it).  Also note that on the Array palette, Build Array will let you combine three arrays together to make a new (bigger) array.

0 Kudos
Message 6 of 13
(3,969 Views)

Hi Altenbach,

 

I have a query that you may be able to answer concerning the array dimensions.  In the example that you sent me I note that the properties of 'raw' [DBL] and also 'DPE' [DBL] are 2 dimensional arrays.  When I tried to further develop this I produced a matrix, called 'xkk' within MathScript which was the same dimension as DPE (i.e. a (3 x 1) matrix) however when I take this outside the MathScript node and loop it becomes a 3-dimensional array ?  I cannot see the difference between 'DPE' and 'xkk' so why is one a 2-dimensional array whilst the other is 3-dimensional ?  I have attached my vi.

 

I'd also like to ask for your advice about how I can build an array within MathScript that takes the values of the inputs Q1, Q2, L1, L2, L3 since I will need to work on various rows of array data within MathScript i.e. I will need to use data from the k+1 sample and the k sample in calculations.  Is there a way to build and refer to arrays within MathScript ?

 

Regards,

0 Kudos
Message 7 of 13
(3,943 Views)

As a frustrated user (who has already tried looking at the LabVIEW documentation and examples without success) I cannot get the output DPE to produce anything more than the first loop calculation despite the loop going round 1000 times.  Is there anything I'm missing here ?  Attached vi file.

0 Kudos
Message 8 of 13
(3,927 Views)

You are turning your DPE into a 3D array, one plane per iteration.

0 Kudos
Message 9 of 13
(3,921 Views)

Ok, thanks I can see what has happened now that I scroll through the index values, I didn't want to do that!!!  Can I turn it back into a 2-dimensional array ? (I tried Resize Array without success) or do I need to do something else?

0 Kudos
Message 10 of 13
(3,913 Views)