Showing results for 
Search instead for 
Did you mean: 

Change Array Element

Go to solution

Quick question-  Is there a way to change a value of an array element without having to re-write the entire array.  For example, an array of boolean values and I want to just change index 3 on or off.  Can I do this without changing it and then writing the whole array to reflect change?

0 Kudos
Message 1 of 8

Use the Replace Array Subset VI.

Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 2 of 8

That does the same as the rest of the array blocks and outputs the entire array.  So if I had an array of 10,000 values and change just one then for this to reflect in the array I have to write all 10,000 values.  I was looking for a way to change an element and it reflect in the array without having to write over the entire array

0 Kudos
Message 3 of 8

Wrap your update in an In-Place element. That will allow the compiler to do the replacement without making a copy. However, you still need to pass the array out on the wire since LabVIEW is a By-Value language and the wire represents the value. Eliminating copies of the data is a goal. The In-Place element will ensure you don't make a copy of the data.


The other option would be to use a DVR (data value reference). That way you would update the contents of the array in memory and would only pull the array out and put it on the wire when you actually needed it.


What is the actual problem you are trying to solve?

Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 4 of 8

Thanks Mark.  I have been using in-place element structure and requires the same thing in that I still have to write the changed array to the variable when done.  Not really an actual problem I am having on this but just seems to make my code less readable and is adding steps that I am not used to in other programming softwares.  I was just hoping there was a way to access and change a specific element/index.  I am using a ton of complex arrays and For Loops and just seems like a waste of processing power and programming time to write the entire array just to change the value of one index/element.  Maybe other softwares I have used do the same thing behind the scenes I am not sure just seems like a waste.  See example attached 

0 Kudos
Message 5 of 8
Accepted by topic author Trinity32244

You want to change the value on an element of an array. If you don't do something with the array afterwards, then you didn't do anything at all (practically - you did something, but there was no point). Therefore, presumably you always have some use for the array after you've changed an element (display, further processing, etc).


When you use the Replace Array Subset, you're only writing to the parts of the array that change. If you use the In Place Element Structure, you've giving an even stronger indication to the compiler that it should not copy the entire array.


If you prefer, you could use Shift Registers to change the appearance (and probably execution speed) of your loop. That would avoid the need for the Local Variables, which seems to be what you're concerned about.




If this example isn't only an example, then you should be far more concerned about the loop iteration rate consuming all of your CPU (there is no wait).

Either implement a short wait (10-50 ms will massively reduce CPU usage but have no discernible effect on responsiveness) or use an Event Structure combined with a button to change the values (or value change on the existing "value to send", but that might be problematic if you wanted to make several true...)


In the above snippet, I'm "writing" to the Array indicator every iteration only in order to update the display. If you have some other use for the array (not an indicator) then you'd just pass it there (no need to update an indicator unless you want an indicator, of course).

Message 6 of 8

Thanks for the reply.  I figured this was the case and have to write to the variable after making changes.  Yea was a quick example and forgot to throw the wait in there. 

0 Kudos
Message 7 of 8

@Trinity32244 wrote:

I figured this was the case and have to write to the variable after making changes. . 

Cbutcher is rewriting the indicator, which has nothing to do with any "variables". The array is entirely contained in the shift register and the indicator just gets updated asynchronously and  at leisure in the UI thread. An indicator requires additional data copies of course, because the indicator mechanism need to store and buffer the data in order not to hold up the rest of the code. Your misguided use of local variables cause additional data copies for the similar reasons, so don't do that. Most of the time, the wire is the variable. Smiley Wink


Indicators are intended for the user, not to "store" data that should be confined to the diagram alone. For many algorithms it is sufficient if the data is in the shift register, which is one of the most efficient ways because most of the time things can operate fully in-place if the sizes don't change.


And no, the "in place element structure" just complicates the code here. Using "replace array subset" does exactly the same and also operates fully in-place. Don't underestimate the intelligence of the compiler. (The in place structure is useful if you actually need the value of the existing element too, e.g. if you want to sum the new and existing element at a given position. In the above code, the output terminal for the existing element is not even wired! Why recruit that extra functionality if it is not used?)


"Replace array subset" is universal and can be used similarly to replace a row or column in a 2D array, a plane in a 3D array, etc., so don't employ exotic constructs (IPA) just to re-learn a different method once things change.


Here's all you really need (top code). And yes, there is no need to spin the loop or update the array unless one of the controls change, so learn about event structure (bottom code). 








LabVIEW Champion. It all comes together in GCentral GCentral
Message 8 of 8