09-03-2018 03:16 AM
Hello!
I'm new to Labview. Trying to add element to an array (in my example it is a string VS array of strings) with a push of a button.
How do I make it work?
My example attached.
Thank you!
09-03-2018 03:41 AM
09-03-2018 05:10 AM
I would suggest you rethink your example. As it stands, at best it will add a constant (here "element added") to your array, and do it (or not do it) once. Not very interesting.
How about expanding the example to do the following:
This is clearly an Event Loop with one Numeric Control (let's call in "Number") and two Boolean Latch Switches (let's call them "Undo" and "Finished"). The Array will be carried in a Shift Register, and we will view its contents using an Array of Dbls Indicator ("My Numbers") placed inside the While Loop between the right walls of the Event Structure and the While Loop. [Do you understand why I placed it there, rather than outside the While Loop?].
You know how to initialize this Shift Register with an empty Array. Let's consider how to do each of the Bullet Points. Typing into the Number control (Bullet #1) is more-or-less "built in" to LabVIEW, and when you push "Return", all you need to do is to add the value in Numbers to the Array wire in the Shift Register. As was pointed out in the previous Post, this wire must be connected in the other Event Cases as well, otherwise the Shift Register will be "dumped" when you choose another Control.
One thing I would advise for Numeric data is to right-click the Control on the Front Panel, choose Visible Items, and remove the check mark next to "Increment/Decrement", which pretty much forces you to enter numbers by typing them and either clicking outside or typing <Return> to end the entry. Why do I say that? Try it both ways and decide for yourself.
Bullet Point #2, as noted, "takes care of itself". How do you do Bullet Point #3, removing an existing entry? Is there a function on the Array Palette that will remove an Array Element? And what if the Array is already empty? [Exercise left for the Reader]. I also think Bullet Point #4 you know how to do, as well.
Try it out.
Bob Schor
09-03-2018 05:24 AM
Thank you for your answers.
I managed to solve my problem. I wanted to see the added element each time I push the button, not as in my previous example code, all of the elements added when I stop running the code (if I indeed rewire inside the cancel event).
My new code attached, if someone is interested.
09-03-2018 05:25 AM
Thank you. I will try playing, as you suggested.
09-03-2018 07:49 AM
It is a Good Thing I'm not "grading" your Solution. You went from using a Shift Register to hold a "variable" (the way LabVIEW is designed to work) to using Local Variables to read and write an Indicator, something that LabVIEW "newbies" often do because "they don't know any better", but which is generally considered "bad practice" for several reasons (something called a Race Condition, which arises because the Control or Indicator can be changed in multiple, completely disconnected places in your code, asynchronously, in almost random order, hence the results are somewhat unpredictable; additionally, finding all the places where local variables make changes can be challenging in larger programs). I would certainly return this "Solution" to you and ask you to re-do it using a Shift Register and no Local Variables. The first Responder to your post was telling you how to "fix" your Shift Register (which you failed to wire through in all the cases, leaving a "Use Default if Unwired" connector, thereby setting the output to the Default, an Empty Array, when you pushed the "OK" button). So you didn't even understand how the Shift Register worked when the "fix" was provided to you!
Bob Schor
09-03-2018 12:23 PM - edited 09-03-2018 01:04 PM
In addition to what Bob just said, you also need to familiarize yourself with the mechanical action setting of booleans. Both of your buttons should be "latch when released" (right-click...mechanical action...). This way they reset back to false automatically once the code has read the new state. No local variables needed. To ensure that the terminal gets read by the code after one has been pushed (even if nothing is connected to it!), their terminal needs to be placed in their respective event case. This also makes them, and their associated event easier to find during debugging. Just scattering the terminals randomly outside the main code is a mess.
The best place for the "array" terminal is right next to the left shift register (Which is initialize with an empty array). This way it will clear immediately when the program is started and before any event have fired, greatly simplifying the code and eliminating all local variables (see also slides 19-24 in my 2018 NI-week presentation. What I meant is on slide #24)
You can also delete the FALSE constant in the main event case. Output tunnels are "use default if unwired" by default so all event that don't output a TRUE can be left unwired, saving you some effort and reducing clutter.
So please go back, follow our advice, and re-do and re-post your new, stellar, and flawless code. A good exercise! 😄
09-04-2018 08:52 AM
With your help, here is a proper solution.
Thank you!
09-04-2018 09:00 AM
Of course I forgot to wire the shift register inside the cancel event.
Try 4 attached.
09-04-2018 10:32 AM - edited 09-04-2018 10:40 AM
Good! 😄
A few minor things: