LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Can you programatically assign cluster size when using Array to Cluster function

Solved!
Go to solution

I'm using the Array to Cluster function.  The only way I know to size the cluster is to right click on the function and set the Cluster Size.  But what if the length of my array changes?  Is there a way to make the cluster size match the number of elements in the array?  Seems like labview should do this automatically at run time.  Maybe there are some property nodes that I'm not aware of.

 

- tbob

Inventor of the WORM Global
0 Kudos
Message 1 of 11
(5,528 Views)
No.  Because the size of the cluster needs to be determined at compile time since it is an inherent part of the datatype.
0 Kudos
Message 2 of 11
(5,526 Views)

What type of data is in your array?  If it's fixed-size elements (numeric, boolean, or a cluster containing only fixed-size elements) you can use the Typecast function in place of Array to Cluster.

 

EDIT: I should clarify this slightly.  You still need a cluster of the right size as an input to Typecast in order to get the right output, but if your cluster is a type definition and you don't want to update the array size every time your cluster changes, you can wire a constant of that type definition and it will work the way you want.

Message Edited by nathand on 04-10-2010 09:07 AM
Message 3 of 11
(5,500 Views)
Array to cluster requires all elements to be the same type, so what do you need the cluster for that cant be done directly with the array?

/Y
G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 4 of 11
(5,464 Views)

I'm using the Insert Into Database function.  The input requires a cluster.  The help screen says it can be an element or a cluster, but I found out that an array does not work.  I have to change the array to a cluster.  I was trying to make it generic enough to use with different databases, i.e, different number of columns.  Can't find a way to do it.  So I will just have to make it work with a specific database, and change the function for databases of different number of columns.  I wish the Insert to Database function would work with an array input.

 

With a cluster input, the first element gets written to the first column, the second element to the second column, and so on.  With an array, the first element goes to the first row and first column, then the next element tries to go to the second row, first column, but an error is generated because the number of columns in the database does not match the input.  In other words, the input is looking for a cluster with the number of elements being the same as the number of columns in the database.  I have a 4 column database.  I cannot put an array with 4 elements into the function.  It doesn't work.  It gives me the error I mentioned.  I don't think there is a solution to this.  I cannot make it generic.  The vi must be written specifically for the database.

 

- tbob

Inventor of the WORM Global
Message 5 of 11
(5,432 Views)

To be able to work it backwards are you have tried we are faced with the challenge of getting LV to update the resulting cluster wire to reflect the number of fields.

 

The closest I got was to turn the challenge inside out "Given a cluster compose a SQL statement to do the update". That was all I had to do was update the type def for the cluster and make sure the elements in the cluster had names that matched the DB. I suspect this is what the NI toolkit does.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 6 of 11
(5,417 Views)

Ben wrote:

..."Given a cluster compose a SQL statement to do the update". .... I suspect this is what the NI toolkit does.


It is, basically, except it uses a separate array for the column names or, if that array is empty, uses the order of the cluster. It also creates an ADO parameterized query, as opposed to building the SQL command, but the basic concept is the same - it parses the cluster in a similar way to what your VI server (or the OpenG variant VIs) VIs do.

 

 

tbob, because an insert operation has to be atomic (the whole row has to be inserted at once), you're going to have to build the SQL query yourself and execute that query using the execute query VIs which should be in the advanced palette. Luckily, this is very simple, so you can easily build a VI which will do this for an array of strings, or an array of numerics, etc. I suggest you do a search for an SQL tutorial to see the exact format of the command.


___________________
Try to take over the world!
Message 7 of 11
(5,406 Views)
Solution
Accepted by tbob

I've tried SQL statments, but it still boils down to having to know how many columns there are in the database before runtime.

 

What i did was to build the object to be a cluster to match the database fields.  My recordset is an array of objects.  Then I have a member vi to build a recordset from the database data, and another member vi to retrieve an array of clusters from the recordset.  If the database changes, I have to change the object and these two vi's.  All other subvi's call these two for data manipulation.  No other subvi's have bundle and unbundle functions in them, only the two member vi's.  So a change in databse requires changing one control and two vi's.  Not too bad.

 

 

 

(I'm tooting my own horn by selecting myself as the accetped solution provider.  I learned this bad habit from others here on the forum.  Smiley Tongue)

Message Edited by tbob on 04-12-2010 11:06 AM
- tbob

Inventor of the WORM Global
0 Kudos
Message 8 of 11
(5,386 Views)

tbob wrote:

 

What i did was to build the object to be a cluster to match the database fields.


That's what you're expected to do, because you would always know which elements exist in a table. That's why the function accepts a cluster.

 

You can still set an unknown amount of columns at run-time, but the table will have to support them, so it's basically pointless.


___________________
Try to take over the world!
0 Kudos
Message 9 of 11
(5,380 Views)

This is for anyone with the same issue reading this long forgotten thread.

 

The solution I came up with was making a case structure that determined the number of columns to be updated and have an Array to Cluster with the corresponding cluster size in each case, total of 32 cases but any amount is possible. This solution is not optimal but it enables you to update a wider range of SQL tables with the same VI and not have to change code if a different table is being written to.

 

This came from a project where I needed to update several different tables within a database with number of columns ranging from 3 to 21 and didn't want to write individual VIs for each. 

 

Hope National Instruments eventually make the Cluster Size in Array to Cluster programatically modifiable.

Download All
0 Kudos
Message 10 of 11
(3,941 Views)