LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Unions in LabVIEW

Does anyone know how i could implement a union in labview? i may not know until runtime what data type the elements will be.

Is it possible to use either variant data or the flatten to string functions?

Has anyone done anything similar?
0 Kudos
Message 1 of 6
(6,897 Views)
Since every wire in LabVIEW must have a defined type, you won't be able to implement a union with traditional structures and wires. However, you can use the Variant type and add attributes (that could be structures) and get the attributes (including get all). You could also use XML to manage an 'bag' of data. In LV 6.1 there are more functions that allow you to tear apart the various type definitions programatically and built a union manager if your need is great enough.
Stu
Stu
0 Kudos
Message 2 of 6
(6,897 Views)
I assume that what you mean by union is the term used in set theory in Mathematics.

LabVIEW does not have built in support for sets and set operations. The closest thing we have is the cluster which will group controls. This is similar to a set. You can combine clusters into one cluster (union) with bundle. Intersection will need to be coded by you.

You can also create strings to represent your sets. For instance each line would be the value of one of the elements. This way you can use the built in string functions to manipulate the strings. It will be slow, but I think this method will be the easiest to code.

Jeremy Braden
National Instruments
0 Kudos
Message 3 of 6
(6,897 Views)
I was thinking of unions as in c, like a struct but it only holds one element at a time
0 Kudos
Message 4 of 6
(6,897 Views)
Hi Matthew,

Unions do not come naturally in LabVIEW.

The closest thing is the "Type cast" function (see advanced>>>data manipulation).

To use it, (like in C) you will have to determine which representation of the data structure you will be using. Use the results of this decision to select a "case" that has the appropriate definition wired to the "type" input.

General comment:
LV works very hard to isolate the developer from the nitty-gritty details of the data representation. If you are working with a block of data that can take on a variety of definitions (like in a union) you will have to dig into those details.

Ask if you have more questions,

Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 5 of 6
(6,897 Views)
> Does anyone know how i could implement a union in labview? i may not
> know until runtime what data type the elements will be.
>
> Is it possible to use either variant data or the flatten to string
> functions?
>


If you are looking for a way to have a tagged union, then do the
following. Build a cluster that contains subClusters of each of your
possible types. Give the clusters names, and then add an enum or a ring
with the names of the clusters. At runtime use the enum/ring to tell
you which cluster has valid data. When writing to this, be sure to set
the ring/enum correctly. This now works the same as an enum except that
it doesn't overlap the data in memory. For most cases this works fine.

It is also possible to have common elements of the
clusters shared by
pulling them out into another common cluster that everyone uses, but
this is done manually and is only needed as an optimization if you feel
that it is worth it.

Greg McKaskle
Message 6 of 6
(6,897 Views)