LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Insert into Array is not delivering the expected array

Solved!
Go to solution

I have two SubVIs where Data Acquisition takes place, the first one I am using shared variables to unbundle data from different clusters and the second one( updated one) I am using queues to dequeue the values from a certain cluster as its important to my application not to lose any samples. I am using insert into array function along with shift registers to build an array with the values from different iterations that should later be sent to Data Logging loop. The problem I am facing is that with the updated SubVI I am getting blank arrays after the insert into array function but with the old one I get arrays with the expected values. 

I am not sure where the problem lies. I uploaded both subVIs, I can't upload the whole project so hopefully those are enough to give me the answer I need.

0 Kudos
Message 1 of 20
(1,920 Views)

The correct function to use to append an element to the end of the array is "built array". No need to keop track of [i].

 

Why do you initialize the shift regsiter with an array containing one element instead of an empty array?

 

There are lots of questionable constructs. Wouldn't it be much easier to have a single shift register with a 2D array where you append an entire row with each iteration?  You did not attach your subVIs, but wouldn't it be more reasonable to append the new data to a file instead of writing all data (mostly redudant) with each iteration?

0 Kudos
Message 2 of 20
(1,913 Views)

You have the i terminal wired to the Insert Into Array, that makes me beleive you want to put the new values to the end of the array.

 

Is that true?  If so, why are you using Insert Into Array?

 

The correct function would be Build Array.  Honestly, when working with arrays, I'll use Build Array more than 99% of the time, and Insert Into less than 1%.

 

The problem with your new one is that the i value increases on every iteration of the while loop, but because of the case structure, you are only "inserting" data on 1 of the 4 cases.  So you get blank values when you insert into index 0, another case runs that does nothing (i=1), then run the acquire case and a value gets inserted into index 2 (i=2).

 

Also Build Array is a lot more intuitive and has fewer inputs.

0 Kudos
Message 3 of 20
(1,911 Views)

You are making it really hard on yourself...

 

  • You're using 'i' to insert data.
  • 'i' increases for all the other cases as well. 
  • So, 'i' is unrelated to the array size.

Why not simply use Build Array?

 

It might accidentally work (hard to say without seeing it all and being able to run it), but it isn't pretty.

Message 4 of 20
(1,897 Views)

wiebe@CARYA wrote:

You are making it really hard on yourself...

 

  • You're using 'i' to insert data.
  • 'i' increases for all the other cases as well. 
  • So, 'i' is unrelated to the array size.

Why not simply use Build Array?

 

It might accidentally work (hard to say without seeing it all and being able to run it), but it isn't pretty.


More importantly, once [i] is larger than the array size (i.e. after a few "non-acquire" cases have occurred), it will no longer insert anything because the index is not valid.

Message 5 of 20
(1,892 Views)

@RavensFan wrote:

 

Honestly, when working with arrays, I'll use Build Array more than 99% of the time, and Insert Into less than 1%.

I don't know that I have Ever used Insert Into Array, so I definitely concur with this statement.

0 Kudos
Message 6 of 20
(1,880 Views)

@johntrich1971 wrote:

@RavensFan wrote:

 

Honestly, when working with arrays, I'll use Build Array more than 99% of the time, and Insert Into less than 1%.

I don't know that I have Ever used Insert Into Array, so I definitely concur with this statement.


Insert Into Array works well for 2D arrays though.

0 Kudos
Message 7 of 20
(1,877 Views)

@altenbach wrote:

wiebe@CARYA wrote:

You are making it really hard on yourself...

 

  • You're using 'i' to insert data.
  • 'i' increases for all the other cases as well. 
  • So, 'i' is unrelated to the array size.

Why not simply use Build Array?

 

It might accidentally work (hard to say without seeing it all and being able to run it), but it isn't pretty.


More importantly, once [i] is larger than the array size (i.e. after a few "non-acquire" cases have occurred), it will no longer insert anything because the index is not valid.


Agreed, but that might not happen.

 

There's 1 element in there, then there's one message (start), and after the stop it doesn't matter anymore.

 

So it could 'work' as it is, but even if it does it's very fragile (understatement of the year). Any change to the initial array or the messages will ruin the code, and the new bug will seem completely unrelated to the changes.

0 Kudos
Message 8 of 20
(1,873 Views)
Solution
Accepted by topic author Amr95

wiebe@CARYA wrote:
Insert Into Array works well for 2D arrays though.

Yes, if you want to e.g. insert/append columns. To append rows, built array is still the correct choice. I have not tried higher dimensions, but you can extrapolate. 😄

 

Back to the original, here are two code fragments that work identically (the formatting can be further streamlined, but I left it discrete because the data in the original problem comes from many different places).

 

As I said, all we need is to append one row to a 2D array for each iteration. No need for all these shift registers, transpositions, etc.

 

Here's a comparison. Both codes give the same result, but one is arguably simpler. 😉

(The left side is modeled after the OP code. Probably fit for the Rube Goldberg thread.... )

 

altenbach_0-1600274665216.png

 

Message 9 of 20
(1,866 Views)

@Amr95 wrote:

 The problem I am facing is that with the updated SubVI I am getting blank arrays after the insert into array function but with the old one I get arrays with the expected values. 


I'm not convinced that we're hitting the problem.

 

If 'i'\Insert Into Array is the problem, I'd expect no data at some point, not 'blank arrays' (probably blank array elements?).

0 Kudos
Message 10 of 20
(1,852 Views)