Showing results for 
Search instead for 
Did you mean: 

Making an array in a loop

Go to solution

I am trying to get data outside a while loop when its running and trying to use local variables to do the same. I've read community answers on similar questions and realized I'm doing something horribly wrong. But I can't find a solution. I've attached a snipet of my code.




Basically I need to take data from a device which is running in a while loop and make a 1D aray of all the data values it throws out as the loop is running. I tried using local variables (array and output array) with an Insert in array block as shown( its almost blasphemous) but it does not work. How can I achieve this? 

0 Kudos
Message 1 of 15

To get help you are better off posting your actual VI rather than screenshots.


You could use local variables (Although not in the way I think you are trying to use them, attach your Vi and I will be able to tell you for certain).


I would suggest either looking a producer consumer loops (Queues) to learn how to transfer the data, and shift registers for storing the array of data you are generating. 


If the producer consumer loops are too complex at the moment you can get away with a shift register and local variable.


Something to note, if you don't limit the size of your array it will keep on growing indefinitely. This will lead to you running out of memory eventually.



Message 2 of 15

My Complete VI might be too big and irrelevant I am posting the whole loop here with another solution I am trying( which again does not work). So there are 4 loops in total. You can see 2 here. This code is used to do a Spiral scan out of a pair of 2 stepper motors and read optical power out which I would want as an array outside. Eventually I need to find the maximum power and its corresponding location in the Spiral Scan. 




0 Kudos
Message 3 of 15

Typically, you would use an initialized shift register and a "build array" node.


Here's a simplified example:




LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
Message 4 of 15

You can't "Insert into array" outside of an array, so use a Build array instead as Altenbach mentions.

G# - Award winning reference based OOP for LV, for free! ADDQ VIPM Now on GitHub
"Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
Message 5 of 15

@Yamaeda wrote:

You can't "Insert into array" outside of an array, so use a Build array instead as Altenbach mentions.

(Talking about the code in the first picture) They were actually inserting at position=0, i.e. prepending to the existing array. (If this is the desired behavior, we can just swap the inputs to my built array). The problem with prepending new values is that this significantly more expensive for the memory manager. Never recommended!


The code in the second picture is pointless, because the array building is outside the loop and only happens once, probably before the while loop even starts. Unfortunately, we cannot see the entire code but I suspect that it is full of mistakes and race conditions and could benefit from an expert analysis. (Unfortunately, the OP thinks sharing the code is irrelevant. 😞 )

LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
Message 6 of 15

Hey, Thanks a lot. I think I've almost got it. Tried your method just replaced shift registers with feedback nodes as my diagram is already too clumsy.  Here's a simplified example VI of what I'm trying to achieve. 


The only problem is that after it enters the second loop structure instead of writing to 3rd element in the array it rewrites over the second element, then moves to the 3rd element. Any workaround this? 



0 Kudos
Message 7 of 15

I do not understand why you are using two while loops sequentially. Attached VI should deliver what you need, adding to what said.



0 Kudos
Message 8 of 15


 thanks a lot for proposing to have a look at my complete VI. I was just scared people would call me out for putting a big VI up there. Happens in other tech forums where people just want you to ask your very specific question and leave. The LabVIEW community is different and that's awesome. So I am attaching my complete VI snippet here. I have attached a code snippet here. Though I don't think you will be able to open it as it uses .NET references to methods given by Thorlabs to operate their Motion Control equipment.


I have yet to make changes to the array data as given above. Pretty close to a solution but would really appreciate some general feedback on my VI as this is my first LabView project and my professor has no idea about the software. 


 Okay so outside my loops I,m just enabling and disabling the motors. Inside the 4 while loops I am trying to trace a square spiral using 2 motors one for the X and other for the Y axis which looks something like this



with initial position (2,2) which then increments in steps of 0.001

the distance at which the angle changes( rather shifts from motor X to Y ) increases by 0.002mm. Something like this:


 Finally I'm graphing my output with a Build XY Graph.

I am controlling the program execution using the error line thus trying to avoid Race condition. 


Thanks in advance. 










0 Kudos
Message 9 of 15

You will get better response if you can attach .VI file. Just saying.

0 Kudos
Message 10 of 15