From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Any way to "index" a cluster?

Hi,
Is there any clever way to handle a cluster as if it were an array? It
would be very useful if I could do things like size and index clusters. It
seems to me that since there is a certain ordering of the elements in the
cluster, it might be possible to find the number of elements in an
arbitrary cluster passed to a sub-vi.

Specifically, I'd like to pass an arbitrary cluster into a for loop, have
it auto-index, and unbundle an element at each loop iteration. Is anything
like this remotely possible?

TIA,

Chris
0 Kudos
Message 1 of 16
(6,723 Views)
Christopher Hodge wrote in message
news:8p2sej$ncv$1@news3.bu.edu...

> Specifically, I'd like to pass an arbitrary cluster into a for loop, have
> it auto-index, and unbundle an element at each loop iteration. Is anything
> like this remotely possible?

Nothing simple. Certainly you can't pass an arbitrary cluster into a for
loop and have it unbundle element by element as you suggest- how would the
problem of multiple datatypes passing along the same wire inside the for
loop possibly be handled?

I've never tried doing it, and it'll be a pig, but check out the "Flatten to
string" and "Unflatten from string" functions. With these you could
conceivably write a function that indirectly takes an arbitrary datatype,
including a complex cluster, processes i
t and returns a similar datatype.
Whether the significant effort would be worthwhile is dubious.
0 Kudos
Message 2 of 16
(6,720 Views)
[posted and mailed]

craig.graham@lancaster.ac.uk (Craig Graham) wrote in
<8p2tgl$bu0$1@sponge.lancs.ac.uk>:

>
>Christopher Hodge wrote in message
>news:8p2sej$ncv$1@news3.bu.edu...
>
>> Specifically, I'd like to pass an arbitrary cluster into a for loop,
>> have it auto-index, and unbundle an element at each loop iteration. Is
>> anything like this remotely possible?
>
>Nothing simple. Certainly you can't pass an arbitrary cluster into a for
>loop and have it unbundle element by element as you suggest- how would
>the problem of multiple datatypes passing along the same wire inside the
>for loop possibly be handled?
>
>I've never tried doing it, and it'll be a pig, but check out the
>"Flatten to string" and "Unflatten from string" functions. With these
>you could conceivably write a function that indirectly takes an
>arbitrary datatype, including a complex cluster, processes it and
>returns a similar datatype. Whether the significant effort would be
>worthwhile is dubious.
>

I don't see how there is any advantage to this? Presumably the reason for
using a for loop is to allow similar processing of each element. In that
case I suspect that the cluster should therefore be composed of similar
elements. Processing clusters of "same" elements from cluster to and from
arrays are tools already built into LabVIEW.

Since you can already do this for clusters where all the elements are the
same then one way to do it for clusters with disimilar elements is to use
clusters that contain themselves two similar elements. As suggested above
one of these elements would be a "flattened to string cluster". Element 1
would be a NAME as a string and element 2 is a string representing the
FLATTENED CLUSTER.

To access the sub-elements in the FLATTENED CLUSTER, a case structure
within the for loop could handle the various unflattenings dependent on the
NAME of element 1. Simlarly because we are working with the same data types
(two stings) within our cluster, you can easily write VI's that will add,
remove or resize as needed using the tools already provided.

Another neat trick is to create templates and store these in your user.lib.
You could create a generic template that would allow you to re-use the
decoding VI's that give access to the sub-elements.

cheers, Alex



--

Alexander C. Le Dain, PhD
ICON Technologies Pty Ltd
http://www.icon-tech.com.au
0 Kudos
Message 3 of 16
(6,722 Views)
I looked into something like this a while back (I can't remember why now)
and found that doing this should work, using the 'type string' terminal to
identify what kind of data format you're dealing with. The big problem if I
remember correctly is that NI has reserved the right to change how the type
descriptor will work in the future. Which mean that your code could
potentially only work on the version of LabVIEW you designed it in.

Hope this helps

Denis

"Craig Graham" wrote in message
news:8p2tgl$bu0$1@sponge.lancs.ac.uk...
>
> Christopher Hodge wrote in message
> news:8p2sej$ncv$1@news3.bu.edu...
>
> > Specifically, I'd like to pass an arbitrary cluster into a for loop,
have
> > it auto-index, and unbundl
e an element at each loop iteration. Is
anything
> > like this remotely possible?
>
> Nothing simple. Certainly you can't pass an arbitrary cluster into a for
> loop and have it unbundle element by element as you suggest- how would the
> problem of multiple datatypes passing along the same wire inside the for
> loop possibly be handled?
>
> I've never tried doing it, and it'll be a pig, but check out the "Flatten
to
> string" and "Unflatten from string" functions. With these you could
> conceivably write a function that indirectly takes an arbitrary datatype,
> including a complex cluster, processes it and returns a similar datatype.
> Whether the significant effort would be worthwhile is dubious.
>
>
0 Kudos
Message 4 of 16
(6,716 Views)
A cluster's property node contains an item called "Controls []", which generates an array of refnums which can be used to access the cluster elements with the generic property node "Value" item. This refnum array can be indexed by the For loop for individual access. Be sure to forcefully limit the number of loops, because the size of the refnum array may not exactly match the actual number of items in the cluster (I think it grows in single byte increments, so it is typically a little bigger.)
0 Kudos
Message 5 of 16
(6,717 Views)
Arrays and clusters are fundamentally diffferent. In an array, each element is exactly the same type, while a cluster can have a mixture of elements (numerics, booleans, strings, other clusters, etc.). This would definitely prevent auto-indexing, because you don't know what you'll get with each iteration.

If all elements of a cluster are of the same data type, you can convert it into a 1-D array (and back) using "cluster to array" and "array to cluster". This should be suitable for your purpose.
0 Kudos
Message 6 of 16
(6,716 Views)

hi Scott,
I didn't completely understood your method, i am getting the refnums from the controls property, can u tell me how to get the elements one by one and send them to a case structure depending on their type
i have attached the vi to show what i am trying to do

thank you



0 Kudos
Message 7 of 16
(6,454 Views)
Hi akash189,

you used the wrong property from cluster:

But what do you want to do with this?

Ton

Message Edited by TonP on 10-19-2006 05:56 PM

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
0 Kudos
Message 8 of 16
(6,441 Views)

hi Ton,
actually i am trying to make a Vi which takes as input a bundle which can have different types of elements like array of strings, array of double numbers and simple strings and double numbers and then i want to write all the things inside the bundle in mat file format, i have different vi for writing array of strring and double,

what you told is working for string only, i want different cases for array of string and array of double numbers

i am attaching the modified vi

thanks

0 Kudos
Message 9 of 16
(6,423 Views)
Hi Akash,

my code had two states:
one for numeric, and one for string.
I can't do all the work!
You should use the typedescriptor to find out what kind of data you have and how to use it.

Ton
Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
0 Kudos
Message 10 of 16
(6,419 Views)