LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Replace Array Element problem with array of clusters

I’m having a very confusing problem with populating an array of cluster strict type defs.  Attached is an example VI with the necessary ctls.  The shift register has to be un-initialized.  This entire VI will later be used as a sub-VI within a while loop of a main VI.  Since this VI is called over and over again with the while loop, I had to include the single step for loop to create the shift register.  I want the shift register to (on the first loop of the while loop) initialize the array type with the default values.  Any subsequent while loop frame, the shift register should output the updated array from the last frame.    This method has worked fine for many other occasions in my code.  The problem seems to be with the replace element in array function.

I created another VI that shows that the replace element in array with a cluster type does work… but the architecture wasn’t correct for my application.  For some reason it doesn’t work here!

Help please!

0 Kudos
Message 1 of 13
(4,587 Views)
It doesn't work because you can't replace an element that doesn't exist. Even with the uninitialized shift register in that VI you wrote the array whose element you're trying to replace is empty.
Message 2 of 13
(4,579 Views)

You start out with an empty array, so nothing can be replaced, of course. You cannot replace an nonexisting element! What is the correct array size?

 

You have several options:

 

  • PREFERRED: Get rid of the FOR loop and use a globally initiaized feedback node initialized with the array of the desired size.
  • Use the "first run?" primitive to set the array to the correct size in the first call.
  • etc..
Message 3 of 13
(4,577 Views)

I agree with what you said... However... My understanding of how an uninitialized shift register works is that it sets the values of the type to the type's default values.  I believe in the example I sent you I had set the defaults values of the array control type for the first 7 elements to some numerical value, made current values default, and then saved and applied changes.  Therefore, the arrayOfClusters type, in my opinion, should NOT be an empty array in the first executing of the loop using the shift register.  I have attached my example again with the array populated in the control type.

 

the first seven elements are populated and then the 256th index of the array have -1 as the value of the numerics.

 

Obviously... This doesn't work.  Is it then safe to assume that the shift register does not initialize the array to it's default value (populated)?

 

Thanks for everyone's input on this.  I plan on using the approach of using the First Time block (against my will).

 

 

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

PhilipJoeP wrote:

I agree with what you said... However... My understanding of how an uninitialized shift register works is that it sets the values of the type to the type's default values. 


Don't really know where you got this understanding, as that's not how it works. There's a difference between type and value. The type is a 1D array of clusters. The value is empty. Because it's uninitialized. Don't believe me? Place an indicator inside the for-loop to the left of the case structure. What's the value of the array of clusters?

0 Kudos
Message 5 of 13
(4,563 Views)
So you are saying that if (hypothetically) the array was instead a simple numeric strict type def and i set the strict type def's default value to something other than zero (say four) and then use an uninitialized shift register for it in the same fashion we are discussing... the first time through the loop the shift register would not initilize the numeric to the value 4?  What you are saying is the uninitialized shift register would instead either provide an empty numeric (i don't think that is possible) or the value zero?  That doesn't make sense.  I'll try your suggestion in a bit.
0 Kudos
Message 6 of 13
(4,559 Views)

You'd get zero. Why doesn't it make sense? You're not understanding the use of typedefs and shift registers. The shift register is picking up the datatype based on the fact that you have a Replace Array Subset and that you're wiring a cluster of 2 elements into the element input of that function. Where does the array typedef come into this? Nowhere, so why would there be any connection between the typedef (regardless of whether you set default values), and the shift register.

 

Now, if you had created a constant (i.e., a value) from the typedef (which had default values) and wired that to initialize the shift register, then you'd get a shift register populated with data. 

0 Kudos
Message 7 of 13
(4,543 Views)
I tried your method and I agree with you... it does not work the way i was expecting.  But after taking a look at the help documentation again... I don't understand why.  If you search help for “For Loop and While Loop Structures"  there is a section dedicated to Shift Registers "Shift Registers and the Feedback Node in Loops". An excerpt from this section says:   "If you do not initialize the shift register, the loop uses the value written to the shift register when the loop last executed or, if the loop has never executed, the default value for the data type." 

In my case... my data type is an array (arrayOfCluster.ctl).  When I created that strict type def... I populated a number of elements in that array and then made those values defaults in that type def.  When you open up the arrayoclsuter.vi... the indicator shows that the array data type is populated.  BUT as soon as you press RUN, the array is emptied.  Similarly... in the attached example... if I created my own numeric control type def _custome_numeric.ctl), set the type defs default value to 111, and use that in the example... the shift register sets the value to zero instead of the default data type value of 111.  It seems that you cannot use a strict type def with a shift register.

This seems to be inconsistent with what the help documentation explains.  Right?  Throw me a bone here!

 

0 Kudos
Message 8 of 13
(4,542 Views)
Look at my ctl files again.  The arrayOfCluster.ctl type def is an array of type def clusters (cluster.ctl).  The data i'm sending the loop is using the clsuter.ctl type def.  Therefore... in my opinion... the replace array element function shoudl be taking the "data" input (of type def cluster.ctl) and should be then replacing the same type def clsuter in the array at a specific index.  That's oen place the type def comes into play.  The second part is... if I populate the clusters in the array and then make the current values of the array default... then save the type def... and apply changes... then the default values of the arrayofCluster.ctl data type should be a populated array.  So when the shift register runs the first time and has nothing to reference from a previous frame... it SHOULD (according to the help documentation) be using the default values of the arrayOfCluster.ctl type def (in this case.. a populated array.)  What is it you don't understand?
0 Kudos
Message 9 of 13
(4,538 Views)

To be perfectly blunt, I'm not the one who's not understanding here. Smiley Wink

 

While the help could be a little clearer, it's not inconsistent. Let's try this a different way. Do the following exercise:

  1. Start with a new VI.
  2. On the front panel place a cluster of two numbers. If you like you can place your cluster typedef (just the cluster, not the array). It doesn't really matter.
  3. Place a for-loop, and wire a 1 to N, and add a shift register. 
  4. Inside the loop place a Replace Array Subset.
  5. Wire the shift register left/right to the array/output array terminals of the Replace Array Subset function. So far the shift register wire is still broken. Why? Because it doesn't know what the datatype is yet.
  6. Connect your front panel cluster to the new element terminal of the Replace Array Subset function. Voila. You end up with this:

    Example_VI.png

    The shift register now knows what the datatype is. It's an array of clusters. What's the default value of an array of clusters? An empty array. Which is what's being used since the shift register is uninitialized.
Now, where is the array of cluster typedef in the above? Nowhere. And that's why it has no bearing on the matter. Even if I were to create an indicator from your array of cluster typedef on the front panel and connect it to the shift register ouput (rather than right-clicking on the "output" and selecting Create->Indicator), it would not change anything.
0 Kudos
Message 10 of 13
(4,525 Views)