LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Putting single data into 1D array

Measuring muon lifetime I get bytes representing the time elapsed between the impact of the muon and the signal of the electron from the decaying muon.

The express.vi histogram does not give me the opportunity to change the number of bins during the measurement process. How do I collect the data into a 1D array, for analysis with the probability and statistics histogram.vi?

 

0 Kudos
Message 1 of 6
(4,517 Views)

Use "build array" and an initialized shift register (or feedback node). There are plenty examples here in the forum.

0 Kudos
Message 2 of 6
(4,514 Views)

Thank you for your answer

 

I tried this already by starting with an initialized array containing NaN as elements. I used 'insert into array' in a while loop with an initialized shift register, with the shift register wired to the index of the array. The problem is that the measurement byte is obtained from a global.vi, which is continuously present. Therefore the iteration does not 'wait' until the next measurement, but fills the array with the present data value.

 

I apologize for not being very experienced

0 Kudos
Message 3 of 6
(4,463 Views)

Your on the right track with the "insert into array" but keep in mind that if you wish to build a histogram you also need to indicate which bin that data point goes into.  I would most likely do this.

 

Based on the user specified number of bins and the range of the x-axis, initialize an array with 0 at all elements and make an array containing the values for the x-axis.  when a new value is detected, determine which bin it should go in, then use "index array" to extract the current bin value, increment by 1, and then use "replace array subset" to update that bin.

 

Now in regards to your value being updated by a global variable.  How did your previous code handle this?  Wiring the global variable to the histogram vi input would have had the same problem of not actually waiting for a new data point.

 

If you could post the vis for both detecting events and handling the array, we could better help you.  As a quick suggestion, I'd recommend a Queue.  An event detection would enqueue the data, and the array storage vi would dequeue the queue and store the data in your array.  This would limit you to recording 1 data point per event and would even allow for timing erorrs (say 3 events are detected in the time you record one...  the additional events are saved in the queue until you are ready to "record" them).

0 Kudos
Message 4 of 6
(4,452 Views)

Using Insert Into Array is usually the wrong function to use.  It should only be used under certain circumstances.

 

 

1.  If you want to put a new element into an array at either the beginning or the end, then you should use Build Array like Altenbach stated.  One disadvantage is that you are continually growing the array.  If it gets too large, you'll eat up memory and the program will slow down because 1.  You are using up memory.  2.  Arrays must be continuous in memory so if it uses up the available space, the memory manager needs to shuffle the array into a new larger space.

 

2.  If you want to preallocate an array and then put elements into it, then use Initialize Array then Replace Array subset.  Advantage, you can allocate space for large arrays right up front.  Disadvantage, you have to handle the array management yourself if you outgrow that original allocation.

 

3.  Only if you want to insert a new element into the middle of an existing array should you use Insert Into Array.  A disadvantage is that you can have a growing array just like #1.  In theory, you can use this method to also put an element at the beginning or end, but it requires more work on the programmer to make sure the index management is done right.  Many Newbies run into a problem when they use "Insert into Array" to put an element at the end and wonder why it didn't work.  If you put an element into an index that is beyond the end of the array (farther beyond than the first empty element),  the new element won't appear in the resulting array.

0 Kudos
Message 5 of 6
(4,441 Views)

muonlab wrote:

I tried this already by starting with an initialized array containing NaN as elements. I used 'insert into array' in a while loop with an initialized shift register, with the shift register wired to the index of the array. The problem is that the measurement byte is obtained from a global.vi, which is continuously present. Therefore the iteration does not 'wait' until the next measurement, but fills the array with the present data value.


This seems wrong. If you use "insert into array", it makes no sense to initialize an array containing elements, because the insert operation grows the array, leaving the existing values intact.

 

If you know the final array size, you can initialize the array, but then use "replace array subset" to substitute data for existing elements while keeping the array size constant. (This is more memory efficient).

If you don't know the final array size, you would initialize the shift register with an empty array and use "build array" to append elements (you could also use insert into array, but it seems more convoluted. Keep it simple!).

 

You clearly also have a dataflow problem. Maybe you could use a queue instead.

 

It would really help if you could attach your code so we get a better idea what you are doing.


@muonlab wrote:

I apologize for not being very experienced


No need to apologize. We all started out once. You've come to the right place to get help. 🙂

0 Kudos
Message 6 of 6
(4,436 Views)