LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to index arrays from a while loop and average them

Hello,

 

I have writed a VI to control a lock-in amplifier and a power supply.

 

I have a big while loop which in each iteration generates 4 arrays that I put in 2 clusters that go to a XY graph (the lock in measures for a negative current and positive current, so I have a ramp that for example goes from -2 to 2 A and then from 2 to -2 A).

 

So, at the end I have something like this ploted in a single XY graph inside a while loop:

 

-2     0.056     2     0.046

-1     0.052     1     0.048

0      0.050     0     0.050 

1      0.048    -1     0.052

2      0.046    -2     0.056

 

Now I want to average this XY graphs that are being generated in every loop interaction.

 

I tried to index the 4 arrays outside the loop, average each one and put them together as I did inside the loop, but it does not generate any plot.

 

I also tried to index outside the loop the final appended array (the XY graph) but I don't find any way to average it, as the add array elements function does not work with 1D array of 2 clusters.

 

Is there any easy way to average the XY graphs for the number of loops I set on the while loop? At the end I would like to have for example the 3 XY graphs and another graph with the average of the 3 graphs.

 

 

Thank you in advance,

0 Kudos
Message 1 of 9
(846 Views)

Hi DFMarq,

 

whatever you have tried and described in this long text: why don't you attach a VI to explain the datatypes you are using and to provide some test data? Please don't forget to also mention the expected result for the test data provided…

Best regards,
GerdW


using LV2020 on Win8.1+cRIO
0 Kudos
Message 2 of 9
(841 Views)

Thanks for the reply, 

 

As I am a novice programmer I did not use sub-Vi so I have a giant while structure with everything mixed so I think is easier for you if I explain it. At the end I have 4 1D arrays that I want to average outside the while loop N-times.

 

However, I will try to cut the problem structure so you can check it.

0 Kudos
Message 3 of 9
(829 Views)

@DFMarq wrote:

Thanks for the reply, 

 

As I am a novice programmer I did not use sub-Vi so I have a giant while structure with everything mixed so I think is easier for you if I explain it.

A giant while loop is hard to handle, even for advanced programmers! That is why we avoid it...

 

Complex code is, well, complex. Break things down to simple blocks, to make things simple. Especially if you're a beginner. Break the rules when you're advanced, so you at least know how to deal with the consequences.

 

Just a friendly advice, feel free to ignore.

0 Kudos
Message 4 of 9
(812 Views)

@DFMarq wrote:

Thanks for the reply, 

 

As I am a novice programmer I did not use sub-Vi so I have a giant while structure with everything mixed so I think is easier for you if I explain it. At the end I have 4 1D arrays that I want to average outside the while loop N-times.

 

However, I will try to cut the problem structure so you can check it.


I agree with wiebe@CARYA, learn to use subvis. It will make your life a lot easier.

 

Also, a picture is worth a thousand words, and a vi (even a bad one) is worth 10,000 words.

0 Kudos
Message 5 of 9
(792 Views)

So I made a VI that summs up what I have in my original code. Do not pay attention to what it is inside the loops, as it does not have any sense at all. The important thing is the four 1D array strings that go to the XY plot. The arrays have allways the same length in the original VI.

 

What I am trying to do is to average the XY graph (or the 4 1D array strings) the N number of times the while loop runs. For example if I perform 3 measurements now I have 3 plots on the XY chart but I would like to have another XY graph with the average of the 3 measurements when it goes outside the final while loop.

 

I tried to use the resulting cluster string and also to put the 4 1D arrays indexed outside as a matrix but I can not average the arrays no matter what I try to do.

0 Kudos
Message 6 of 9
(755 Views)

Hi DFMarq,

 


@DFMarq wrote:

The important thing is the four 1D array strings that go to the XY plot. The arrays have allways the same length in the original VI.

What I am trying to do is to average the XY graph (or the 4 1D array strings) the N number of times the while loop runs. For example if I perform 3 measurements now I have 3 plots on the XY chart but I would like to have another XY graph with the average of the 3 measurements when it goes outside the final while loop.

I tried to use the resulting cluster string and also to put the 4 1D arrays indexed outside as a matrix but I can not average the arrays no matter what I try to do.


First of all this VI is a mess. Why do you use WHILE loops when you want to use FOR loops for a fixed amount of iterations? Your VI could look like this:

(I hope I did all changes correctly. Please verify.)

 

You are talking about "measurements" in your question: there is no control labelled "measurements". Instead there is "numeric2" and "numeric3" setting the amount of loop iterations: which one are you talking about? You are talking about "the loop" but there are several loops stacked inside of each other: which one are you talking about?

Why did you use mostly DBL when all the loops expect I32 values for iteration counts?

 

Hint:

When you want to calculate an average of the data created in the "middle" FOR loop then you should wire that data using autoindexing tunnels outside of the outer loop. You will get a 2D array for each 1D array inside the loop and can average the data row-wise…

(Ofcourse you can do the summing inside the outer loop and only need to divide by the loop iteration count after that loop. Now you just need a shift register!)

Best regards,
GerdW


using LV2020 on Win8.1+cRIO
0 Kudos
Message 7 of 9
(722 Views)

First of all, thanks for your answer and sorry for the mess.

 

I used while loops because in the big code all the whiles except the outer one (numeric 3) have a variable number of iteractions.

 

The loop I want to use to average my 4 1D arrays is the outer one, the numeric 3 in this case, so if for example I set numeric 3= 4 loops I would like to average the four 1D arrays (or the cluster that goes to the xy graph inside the while) four times. 

 

I tried several things but at the end I did not manage to average these arrays by rows, it allways result in a xy plot with a straight line.

0 Kudos
Message 8 of 9
(715 Views)

Hi DFMarq,

 


@DFMarq wrote:

I tried several things but at the end I did not manage to average these arrays by rows, it allways result in a xy plot with a straight line.


Why don't you show what you have tried so we could provide help to improve your code?

 

Suggestion:

The inner loop creates a plot of 10 samples.

Using the shift register the plots of each iteration of the outer loop are averaged…

Best regards,
GerdW


using LV2020 on Win8.1+cRIO
0 Kudos
Message 9 of 9
(712 Views)