LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Inserting elements to array

Solved!
Go to solution

Good day, I'm new to Labview. I need to build 1D array of 10 elements. I have a numeric control for inserting elements to array. Everytime, if I change a value in numeric control, element should be inserted to array. I'd like to do that with shift registers. Please help. Thank you.

0 Kudos
Message 1 of 10
(7,268 Views)

Your description could be interpreted in different ways:

 

In labview, "Insert into array" means that a new element is inserted at a specified location and the array grows by one element. Often users actually mean "replace array element", which keeps the array at a fixed size and the value of an element at a given position is changed to a new value. Another possible interpretation of you question would be to apend a new element at the end of the existing array. This is typically done using "build array".

 

What does the numeric control do? Does it correspond to the new value to be inserted/replaced/prepended/appended or does it correspond to the positions where you want the insertion to happen?

 

A shift register does not insert anything anywhere. It is used to keep the updated array available fo the next iteration, so yes, you need one.

 

What have you tried so far?

0 Kudos
Message 2 of 10
(7,262 Views)

In my case, I think is right choice using "build array". I try to describe, how it should work. I write value to numeric control, when I do it, it insert value to array to position with index 0, when i change number in numeric control, it insert value to array to position with index 1, etc.

0 Kudos
Message 3 of 10
(7,260 Views)

How is LabVIEW supposed to know when you have entered a new value to be inserted?

Are you allowed to insert two identical values in a row?

 

Maybe the attached very primitive example (LV 2010) can give you some ideas. (... of course you could spice it up with an event structure, etc. but let's keep it simple for now). 😉

0 Kudos
Message 4 of 10
(7,244 Views)

It's nearly what I need. With one change. Without "Insert" button. Value should insert to array automaticly, when the number in number control is changed.

0 Kudos
Message 5 of 10
(7,240 Views)
Solution
Accepted by topic author George.black
0 Kudos
Message 6 of 10
(7,233 Views)

Yes, exactly. I could not figure it out. Thank you very much.

0 Kudos
Message 7 of 10
(7,230 Views)

Of course at this point you want to learn about memory performance optimization and such. While it would not make a difference here, it will later in your career when you suddenly need to deal with huge arrays. Growing an array in a loop can cause performance issues because resizing an array often needs a new copy in memory because arrays need to be contiguous. Constantly growing arrays can lead to memory fragmentation and slow performance due to constant reallocations.

 

In a case such as this where you exactly know the final size of the array, it is better to initialize the shift register with an array of the final size (containing e.g. all NaN) and replace elements with your real data until the array is full. Now the array operations operate fully "in place" because the array size never changes. This can be orders of magnitude faster for large data structures.

 

See if you can implement this idea starting with my example above. See how far you get. 😄

0 Kudos
Message 8 of 10
(7,226 Views)

I made a modification the this code to delete the last element after fill with 5 elements to the array. I just needed it and wanted to share with you guys. 

 

deletes elelments.png

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

Hi pcortes,

 

what's the point of checking for ArraySize >= 10 when you constantly delete the 6th element of the array???

 

Simplified:

check

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 10 of 10
(5,902 Views)