LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Append a value to an array

I am pulling an index value from a device that is connected to a usb, I am then saving that value to a file (along with some arrays from other devices).  I wrote a piece of code that appends the index to an array, but I keep just getting an array of zeros from the saved file.  I can share more of the iv, but the value from the device is pulled and stored as an internal variable x/y.  Then in the for loop an array is initialized and it appends the x/y variable to the bottom of the array.  I should also mention that outside of the for loop everything is encased in a while loop.

0 Kudos
Message 1 of 7
(3,286 Views)

Remove the empty array wired into your For loop and simply use auto-indexing for the output terminal of the For loop to build the array.

 

With respect to your original question the reason the array is because the output for your array terminal is set to last value. In addition, insert into an array must have a non-empty array in order for it to work. So, if you don't allow the For to automatically build the array you would need to replace the array terminals with a shift register and then replace the Insert Into Array with Build Array.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 2 of 7
(3,280 Views)

Hi Bert,

 

please take those "Training resources" offered at the top of the LabVIEW board!

 

  • Your images shows a missing understanding of DATAFLOW: delete all "x/y" local variables and use wires instead!
  • That FOR loop seems to create an array of 1000 elements, all of the same value "x/y": why don't you use InitArray instead?
  • Using InsertIntoArray most often is the wrong function, use BuildArray instead!
  • Why do you write a value into the "x/y" indicator terminal and into a local variable of that very same frontpanel element?

All those items shows missing LabVIEW basics…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 3 of 7
(3,277 Views)

Three problems:

 

1.  You have a race condition because of your local variables.  The X/Y variable in the loop probably gets read before data gets written to it in the left part of your picture.

2.  Only the last iteration of that For Loop matters because you used regular tunnels.  So "inserting" an element at index 999 of an empty array is going to give you an empty array.  Those input and output tunnels for the array should be a shift register.

3.  Don't use "Insert Array" when you want to append and element to the beginning or end of an array.  You should be using Build Array.

0 Kudos
Message 4 of 7
(3,276 Views)

As others have said, if you want help, attach your VI. We cannot debug truncated pictures. (With limited exceptions!)

 

In this case however, your mistakes are so glaring that we can primarily tell that you need to do some training first.

 

  • Wires flowing in all direction and it is hard to tell what's connected to where.
  • Overlapping wires.
  • glaring race conditions due to blatant abuse of local variables.
  • Wires coming from outside the picture. No way to tell their content, meaning, or size.
  • Lots of mismatched datatypes. Coercion dots everywhere!
  • You FOR loop takes an empty array and since only the last iteration matters for a "last value" tunnel, you get an empty array, because it is not possible to insert a value at index 999 unless all lower elements already exist.
0 Kudos
Message 5 of 7
(3,236 Views)

I am still having an issue of getting the frequencies of all the daq systems to match up.  I will get periodic rows of zeros in the outputed text file.  I attached the vi to see if you could help me out.  Basically I have six devices that are connected via usb/ethernet: an x,y,z motioned controlled stages, two eddy current sensors to measure displacement, and finally a load cell.  The load cell is what I am having difficult matching up the frequencies of all the other components.  For the x,y, and z stages there are supplied modules to set the velocity, tell the stage to move, and record the position of the stages (I have taken all these modules and imported them into the iv).  Then for the eddy current sensor, the company already supplied an iv to set up communication and record the displacement readings.  The company with the load cell doesn't supply anything and thus had to create my own.  Please let me know if anything becomes apparent 

0 Kudos
Message 6 of 7
(2,768 Views)

Ah, what a mess. So much very questionable code (see below*). Can you point out where the "load cell" parts are on the diagram? We don't have your hardware or drivers. Maybe you could make a small VI that simulates the problem parts without hardware.

 

 

*examples (just the tip of the iceberg!):

  • I cannot efficiently inspect the diagram because I don't have a wall-sized monitor, just a laptop.
  • why is there so much duplicate code??? Why do you have at least eight string array diagram constant containing a single element of "1", then convert it to upper case? Why? Hmm, what's the uppercase version of "1"???
  • Why do you set the num.rows property for array indicators, even though you only have room to show one element on the front panel?
  • You simply need to learn the bare basics of LabVIEW. Have a look at the Rube Goldberg code fragment below (I rearranged things slightly for better illustration).
    • The FOR loop repeats the same operation 10x. Once is sufficient!
    • if the outer [i] is zero, you get an array with a single element with a value as wired to the element input
    • if the outer [i] is NOT zero, you get an empty array. You cannot insert into a position that does not exist!
    • And why would you explicitly do all this in SGL if you need the output as DBL a nanosecond later?
    • Maybe if you explain what output you actually expect from all this, we could point you in the right direction.

 

altenbach_0-1627144281559.png

 

0 Kudos
Message 7 of 7
(2,517 Views)