LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Adding element to an array when pushing a button

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!

 

 

Download All
0 Kudos
Message 1 of 10
(5,165 Views)

You just have to wire your array trough in the "Cancel" Event.
It would also help to put the button controls into the respective Event case to release them after you pushed them.

0 Kudos
Message 2 of 10
(5,152 Views)

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:

  • Allow you to enter (and edit while entering) something -- I'll use a Dbl (numeric data) as an example.
  • Entering the number will correspond to adding it to the array -- don't need an OK button.
  • An "Undo" button will remove the last-entered number (if any are still there).
  • A "Finished" button will exit from this routine with the final Array showing.

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

 

0 Kudos
Message 3 of 10
(5,136 Views)

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.

 

Message 4 of 10
(5,132 Views)

Thank you. I will try playing, as you suggested.

0 Kudos
Message 5 of 10
(5,131 Views)

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

0 Kudos
Message 6 of 10
(5,112 Views)

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! 😄

 

0 Kudos
Message 7 of 10
(5,095 Views)

With your help, here is a proper solution.

Thank you!

 

0 Kudos
Message 8 of 10
(5,058 Views)

Of course I forgot to wire the shift register inside the cancel event.

Try 4 attached.

0 Kudos
Message 9 of 10
(5,054 Views)

Good! 😄

 

A few minor things:

  • It is very annoying if the front panel and diagram are both maximized to the screen. This means you can only look at one or the other at any given time and constantly switching between the two windows when programming gets tedious. The information content is low if the screen contains mostly white-space most of the time. (Seems you have a dual monitor setup, but still .... Later you might have several subVIs to work on at the same time)
  • You can wire from the cancel terminal to the stop condition. No need to wire from the event data node.
  • It would take very little effort to arrange the controls a little nicer on the front panel and keep the panel origin (The slightly thicker crosshair of the grid) exactly in the upper left corner. Make both buttons the same size.
  • The cleaner you keep the diagram and front panel, the easier it is to expand it in the future without creating a big mess.
  • Instead of having a very long array container, you can make the size more reasonable but show the array scrollbar instead.
  • Instead of "OK" and "cancel", give the buttons more intuitive labels and boolean text, e.g. "Add element" and "stop" or "exit".
  • You really only need one event case. You can assign it to both buttons.
0 Kudos
Message 10 of 10
(5,038 Views)