1d Array Of Cluster Of 2 Elements Labview

0 views
Skip to first unread message

Zenia

unread,
Aug 5, 2024, 1:14:23 AM8/5/24
to searnonpsinli
Im sometimes amazed how lazy I get. As soon as a cool new feature comes out, like a For Loop with Break, I want something better. I'm tired of writing code to search through an array of clusters for some specific value that matches. I really want LabVIEW to do this for me. This could drastically improve coding efficiency in advanced applications.

Imagine we have a new primitive search function that can search an array of clusters for a specific item and return the index of the first match. Then instead of writing this code manually, I could just drop the function and select the type to search by.


Sorry, I am a bit confused what you mean with the polymorphic selector. You are severly overloading of the meaning of the word string in the code you are showing (1:datatype, 2:control name, and 3:cluster element name), so it's not clear what refers to what.


Yes, "String" is a bit confusing here. What that should be is the name of the data type. This should be analogous to what shows up in the Unbundle by Name text. If you were searching for a nested item, you'd see "Parent.Child" or whatever.


On a side note, I think my explanation above of what I want to search for is completely misleading. It should be the name of the cluster element, not the data type at all. I should have used an example with a cluster element called "abc" instead of "string."


I do this a lot in my code only I use a second array with whatever key (in the above example string is used as the key), search that array for the index I want and index the cluster for the element I want assuming that the key array and the cluster array are in the same order. (They had better be.)


Clusters group data elements of mixed types. An example of a cluster is the LabVIEW error cluster, which combines a Boolean value, a numeric value, and a string. A cluster is similar to a record or a struct in text-based programming languages.


Similar to arrays, a cluster is either a control or an indicator and cannot contain a mixture of controls and indicators. The difference between clusters and arrays is that a particular cluster has a fixed size, where a particular array can vary in size. Also, a cluster can contain mixed data types, but an array can contain only one data type.




The previous steps walked you through creating a 1D array. A 2D array stores elements in a grid or matrix. Each element in a 2D array has two corresponding index values, a row index and a column index. Again, as with a 1D array, the row and column indices of a 2D array are zero-based.


Up to this point, the numeric elements of the arrays you have created have been dimmed zeros. A dimmed array element indicates that the element is uninitialized. To initialize an element, click inside the element and replace the dimmed 0 with a number of your choice.


If you wire an array as an input to a for loop, LabVIEW provides the option to automatically set the count terminal of the for loop to the size of the array using the Auto-Indexing feature. You can enable or disable the Auto-Indexing option by right-clicking the loop tunnel wired to the array and selecting Enable Indexing (Disable Indexing).


When you wire a value as the output of a for loop, enabling Auto-Indexing outputs an array. The array is equal in size to the number of iterations executed by the for loop and contains the output values of the for loop.


Be aware that if you enable Auto-Indexing on more than one loop tunnel and wire the for loop count terminal, the number of iterations is equal to the smaller of the choices. For example, in the figure below, the for loop count terminal is set to run 15 iterations, Array 1 contains 10 elements, and Array 2 contains 20 elements. If you run the VI in the figure below, the for loop executes 10 times and Array Result contains 10 elements. Try this and see it for yourself.


Similar to array constants, you can use cluster constants to store constant data or as a basis for comparison with another cluster. Create cluster constants the same way you created array constants in the steps discussed earlier.


If you already have a cluster control or indicator and want to make a cluster constant that contains the same data types, make a copy of the cluster control or indicator on the block diagram and then right-click on the copy and select Change to Constant from the shortcut menu.


Use the Bundle function to assemble a cluster from individual elements. To wire elements into the Bundle function, use your mouse to resize the function or right-click on the function and select Add Input from the shortcut menu.


Use the Unbundle function to disassemble a cluster into its individual elements. Use the Unbundle by Name function to return specific cluster elements you specify by name. You can also resize these functions for multiple elements in the same manner as the Bundle and Bundle By Name functions.


Cluster elements have a logical order unrelated to their position in the shell. The first object you place in the cluster is element 0, the second is element 1, and so on. If you delete an element, the order adjusts automatically. The cluster order determines the order in which the elements appear as terminals on the Bundle and Unbundle functions on the block diagram. You can view and modify the cluster order by right-clicking the cluster border and selecting Reorder Controls In Cluster from the shortcut menu.


These cannot be wired together because their data types (numeric, string, array, cluster, etc.) do not match. Show the Context Help window to see what data type is required.

The type of the source is cluster of 2 elements.

The type of the sink is 1D array of cluster of 2 elements.


Please get rid of the stacked sequence structures. It hides code, makes it hard to read, and causes wires to run backwards. Replace them with Flat Sequence Structures. Once you do that, you may find that you don't need all the frames, or maybe don't need the sequence structure at all.


This had me blinking until I realized you meant "if your cluster have arrays, it will sort by them as well." I couldn't figure out how the sort 1D array was going to sort arrays inside an array of clusters.


That's pretty cool though. I just tried it out and sure enough, with an array as the first element in the cluster, the sort will sort by the array's first element, then second, and so on. Thanks for this tidbit as well!


Hehe... well, it's both actually. The yellow that I have on my BD is a lot lighter, almost white but just enough of a tone to not blind me when I open up my code in a dark environment. I'm one of those that is accustomed to black-lit workspaces. Comes from long hours as a web developer at eAcceleration, where flourescent lighting was an evil demon that drained the lifeforce from you after 18 hours of crunch time coding. I worked in an area that we dubbed "The Dark Side" :ninja:


I'm pretty sure that OpenG has some better array sorting/searcing VIs that let you pass in a VI Server reference to do the comparison, so you just write a compare function that compares the desired element of the cluster and returns greater, equal or less. I saw something like that on Jim Kring's screen when he won the LV speed programming competition at NI Week a couple years ago.


I was playing around with the most basic functions of LabVIEW such as converting single elemtents to arrays and cluster, back and forth. (Don't question the use of the shown algorithm, there is none.)


When converting a cluster (of similar types of course) to an array, using that for a simple mathematical operation, and converting it back to a cluster the following unbundle to splits the cluster into its individual elements behaves strangely. Connecting the unbundle to this existing cluster it automatically increases its size to 9 instead of the expected 3. The probes and the numeric indicators show that the elements above the three first items are zero.


Explanation: The reason for this behaviour is that while the size of an array can vary dynamically at runtime, the data structure of a cluster is fixed at compile time (LabVIEW is a strongly-typed language). The compiler needs to be told the size of the cluster. If you really need to create arbitrary clusters at runtime you can probably do something with variants, but I'd take another look at what you're trying to achieve to check whether it's really necessary.


You can use the Array to Cluster node to do the conversion for you. The tricky part is the name matching. If the first element in your cluster is always going to be the first element in the array, it works fine. But as soon as you add a new cluster element, you will run in to trouble. The Bundle by Name eliminates this problem by using the names and is really the only way to make sure your names always match up with the right array element.


Your problem code is actually right before the red square. Instead of the "array-to-cluster-unbudle dance", you should simply use a plain index array resized for the same number of outputs. same functionality, less convoluted.


In this example, an array of clusters is broken down into its elements with each iteration of the for-loop. Within the for-loop, the isolated cluster is broken down into its data types. Outside of the for-loop,arrays of each individual elements of the same data type are created by auto-indexed tunnels. These are then displayed as array indicators and all of the numeric arrays are displayed as graphs.


The Array To Cluster function converts a 1D array to a cluster of elements of the same type as the array elements. Right-click the function and select Cluster Size from the shortcut menu to set the number of elements in the cluster.


so now I use a 'Build Array' instead of the 'Build Cluster Array' which returs the same thing, basically, BUT one can use that as many times as one needs as a way of sticking a 'rogue' single elements into the general population of the already existing array of elements.

3a8082e126
Reply all
Reply to author
Forward
0 new messages