LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Collecting substantial no of variables

Solved!
Go to solution
Solution
Accepted by topic author lisam88

FormatCluster.png

Something like this should work.

/Y

G# - Award winning reference based OOP for LV, for free! ADDQ VIPM Now on GitHub
"Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
Certified-LabVIEW-Developer
0 Kudos
Message 11 of 17
(681 Views)

@lisam88 wrote:
I'm a bit confused with the last step. Now that the data is set to an indicator, rather than control, how can I feed it into the format into string? And doesn't an array of clusters have to be converted in order to go into a format into string? How do I go from the 1D array to string?

 


Lisa,

     You have 38 Generators, each of which produces 22 Variables.  The Variables are arranged in a Cluster which we defined in a TypeDef.  You build the Cluster by using Bundle By Name, with the TypeDef  "constant" sitting "on top of" (i.e. wired into the top input of) the Bundle-By-Name function, showing you where to wire in the 22 variables.  You now have, coming out of the Bundle By Name, a wire which you can "export" by wiring it to an indicator of your sub-VI.

     Each Generator gives you one such "wire" or Cluster.  I'm not sure how you are gathering the data from all of the Generators, but since they are all the same type (namely our Cluster), you assemble them into an Array of Clusters.

     Now you want to Format your string from these Clusters.  Look at your earlier code, and consider giving it a single Cluster.  You have a Format into String with an Initial String, you hand your Cluster to your sub-VI that "stringifies" the Cluster, then hand this String to the Format Into String with a "(%s), " format.  OK, you now have a "new" Initial String that you can use to repeat the same thing with the second Cluster.

     This can be encapsulated using a For Loop with the Array of Clusters feeding in through an Array Index tunnel (which gives the Array elements, i.e. each Cluster, one at a time).  Inside is Format Into String, with a Shift Register providing the Initial String input and taking the Resulting String output.  So the first time through, the Initial String is your command (minus the String arguments), you add the first string on the end with Format into String, and this becomes the input (via the Shift Register) for the next string.  When you exit, you'll have the entire (long!) string with all of your arguments, except it will have the extra comma (or comma, space) that you can remove, as Yamaeda illustrates.  [I can't illustrate right now as I'm in the process of removing/reinstalling LabVIEW -- I just discovered the original install failed to install the Discovery Service properly].

 

Bob Schor

0 Kudos
Message 12 of 17
(674 Views)

Thanks Yamaeda!

 

I tried unbundling the array of clusters but an get an error stating I can't connect a 1D array of cluster of 22 elements to a sink with cluster of 0 elements. I managed to get it to work using index array function and this allows me to put the data into the format into string, see attached VI. But it only takes data from one generator cluster, so if I set the number of iterations of the outside loop as 38 it returns the data only for gen_id=38. Perhaps this is because of the indexing function? I have limited knowledge so I just tried a few functions to see which would take the array of clusters data type.

 

What I want to achieve is 22 variables for each generator 1-38 inserted into the database in one iteration. PostgreSQL will take the following format:

 

"INSERT INTO gen_test VALUES (timestamp, gen_ID...), (timestamp, gen_ID...), (timestamp, gen_ID...), (timestamp, gen_ID...), etc.."

 

Which I am able to achieve by formatting into string outside of the inner for loop so that there is only one INSERT. My only problem is obtaining the data for every generator. I tried using a shift register with the array of generators but this returned an array of values rather than a string.

 

 

0 Kudos
Message 13 of 17
(674 Views)

You can't unbundle an Array, no. That's why i put it in an autoindexed For Loop (shown by the [] symbol), so you'll look at one row at a time. That's what you achieve with Index array. I can't open your VI as i'm on LV2011 today, but it sounds like you're not autoindexing.

/Y

G# - Award winning reference based OOP for LV, for free! ADDQ VIPM Now on GitHub
"Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
Certified-LabVIEW-Developer
0 Kudos
Message 14 of 17
(668 Views)
Solution
Accepted by topic author lisam88

Lisa,

     You are very close.  Now that I'm back on a machine with LabVIEW 2016, I've modified your code according to my (probably confusing) description.  Here it is:

Database Generator.png

Parts of this you'll recognize as it comes directly from your code.  Let's start with the For Loop and the Array of Generators.  When you bring an Array into a For Loop, the input tunnel "naturally" turns into an "Indexing Tunnel" (if you look closely, the symbol is a square with the characters "[]", suggesting an array, inside it).  This causes the For Loop to "loop over" every element of the Array.  Notice that "N" is not wired -- it automatically does each array element (so you don't need to know the size of the array).  Also notice that the index, "i", is not needed.

 

The first step is to format one Cluster into a String, then insert that String into the (growing) command line that starts with "INSERT INTO" and has the Cluster-as-string in parentheses, followed by ", ".  We bring this to a Shift Register so that when we process the next cluster, this (growing) string will be the "starting point" where we add the second Cluster, then the third, etc.  

 

When we exit, our cluster ends with the final ", ".  Trim WhiteSpace gets rid of the space, and shortening the string by one gets rid of the comma.  The rest you should recognize.

 

See how simple using arrays to manage repetetive code can be when you use Indexing For Loops and Shift Registers?

 

Bob Schor

Message 15 of 17
(662 Views)

It works perfectly with auto indexing, thank you!! I really appreciate you taking the time to help.

0 Kudos
Message 16 of 17
(653 Views)

Thank you for your time bob. You've been most helpful! Have a great day.

0 Kudos
Message 17 of 17
(650 Views)