LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do you use local variables?

Hey everyone! 

 

How do you guys use local variables when programming in LabVIEW?

 

I've been wondering about the ways that local variables are typically used in real-life LabVIEW applications. I'm familiar with how they work and some of the caveats and warnings that come with them, but was interested in specific examples and applications/design patterns in which you've found them helpful. (Keeping this rather vague on purpose to see what examples you all might have!)

 

Any descriptions, use cases, or code snippets you'd be willing to share?

 

Thanks a bunch!

0 Kudos
Message 1 of 18
(16,751 Views)

Most common use cases for me are to initialize or reset the front panel, and in simple test code where a local variable is an easy quick solution. For example, if I'm putting together a prototype user interface and need access to the same control in more than one event case, but not too many of them, I'll use a local variable, since putting the control outside the event case can have unexpected or undesired effects (in terms of reading the control value before an event executes that might affect the value of that control).

Message 2 of 18
(16,733 Views)

Mostly user interface:

Initialize controls on program start

Set value of controls based on program results. Too many different examples. For example unpress button if measurements ended programmatically. 

Modify value of controls, linked together (for example cursor level on graph affects digital control that can be used to set the same level). 

Slide with multiple sliders. (for example one - center, 2 others at fixed width) From value change event find slider that moved and adjust position of 2 others.

 

Not user interface:

When cluster-indicator is not worth making it a typedef, but I use Bundle by name.

Message 3 of 18
(16,726 Views)

I do my very best to make sure there are no local Variables in my LabVIEW code.  Local variables are subject to Race Conditions (since there is no data flow), are hard to trace and locate (since there are no wires), and in most cases can be eliminated in favor of "better ways of doing this".  An example is communicating between two loops, such as using a single Stop button to stop two parallel loops.  I've used various methods to do this, but never (or, at least, not in the last 5 years after I "learned better") a Local Variable copy of the Stop control.

 

Bob Schor

Message 4 of 18
(16,709 Views)

To make a control also and indicator.  When the User changes a control, that causes something who's result is written into the control via a local.  For example, if the User changes "setpoint" of an oven from 500 to 5000 degrees, but the maximum temperture setpoint can be is 800, then the control will show 800, not 5000.  Or if some other process triggers a safety limit on the oven, then that setpoint reduction will be reflected in the "control".

 

This use of locals is done within a single loop, and thus in a defined order with no posibility of race conditions.

Message 5 of 18
(16,693 Views)

I like DrJ's example, which I coded up here:

Good Local Variable.png

 

But then I thought, "This looks like putting a Max on a Control and coercing it to not get too big".  If this is what you want to do, then do it (which explicitly says "I'm making this control to have the following Max value, and you cannot set it bigger!!!").  Here's the other case of this Diagram Disable (enabled for clarity):

Better Local Variable.png

 

Now this is in two pieces.  The first piece of code that sets the Upper Limit goes in the initialization section (and if you want to modify the Safe Temp, you simply do it again).  The Event code now is dirt simple -- just read the value and use it, as you know the values are safe by design (well, by LabVIEW code, anyway).

 

Bob Schor

 

Message 6 of 18
(16,673 Views)

I rarely use local variables, most cases that I use them are similar to DrJ's. Bob S's method of limiting the input also works, but as usual there are edge cases where it does not work, and then a local works well. (For example, a bandpass filter, the high frequency limit should be greater than the low frequency limit.) Race conditions can be avoided with well written code. Just remember that local variables create a data copy, which is fine for scalars, but not for large arrays.

 

Cheers,

mcduff

Message 7 of 18
(16,659 Views)

Too often I see people using local variables when they could just use wires (this creates race conditions).  So, if you can wire it, DO NOT use a local variable.

Message 8 of 18
(16,655 Views)
Message 9 of 18
(16,600 Views)

@Bob_Schor wrote:

But then I thought, "This looks like putting a Max on a Control and coercing it to not get too big".


That only works in simple cases, where nothing else can affect the item of interest except the control.  In more complex cases, particularly once one has started to get separation between 'User Interface' and 'Business Model', and maybe have multiple UI components, it becomes much more valuable (and simpler) to have the UI 'view' the 'model'.

Message 10 of 18
(16,568 Views)