11-24-2011 01:44 PM
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.
Solved! Go to Solution.
11-24-2011 02:26 PM
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?
11-24-2011 02:36 PM
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.
11-24-2011 02:51 PM - edited 11-24-2011 02:51 PM
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). 😉
11-24-2011 02:56 PM - edited 11-24-2011 03:01 PM
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.
11-24-2011 03:04 PM
Like this?
11-24-2011 03:06 PM
Yes, exactly. I could not figure it out. Thank you very much.
11-24-2011 03:14 PM
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. 😄
03-30-2020 08:04 AM
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.
03-30-2020 08:19 AM - edited 03-30-2020 08:23 AM