From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to save data in while loop to array

Solved!
Go to solution

Hi everyone, I'm new to LabVIEW. I'm trying to read data using DAQmx and then calculate the center of gravity of each series of data. I read 20 number each time and then do the calculation work. Now I want to output the result to an array to check the stability, but I get into trouble in how to output the results in the while loop. 

Any feedback would be helpful. Thanks.

image.png

0 Kudos
Message 1 of 18
(4,666 Views)

1. No need for a Timed While Loop.  Just change it to a regular While loop and the acquisition will set your loop rate.

2. No need to wire up the N terminal on the FOR loop.  The Autoindexing tunnel will tell the FOR loop how many times to iterate.

3. Your first value in the read array is multiplied with 0, so it is not actually being counted in your calculation.  Is that desired?

4. Since you are using Continuous Sampling, do NOT wire up the Samples Per Channel on the DAQmx Timing VI.  You are just limiting your buffer and increasing the chance of getting an overflow error by wiring it up.

5. There are a few options for building up an array.  Do you want to compare everything while the acquisition is still happening?  Post process?  Do you want to compare every loop iteration's data?  Or just the last X iterations?


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 18
(4,630 Views)

Thanks for your advice.

Reply to Q3: Yes it is desired.

Reply to Q5: I want to compare every loop iteration's data. I'm sorry for my poor English. To be specific, I can get 20 numbers of data each time and then calculate the center of gravity. I want to save these results into an array and then output it using the DAQmx write.

0 Kudos
Message 3 of 18
(4,622 Views)
Solution
Accepted by topic author chen3477

@chen3477 wrote:

To be specific, I can get 20 numbers of data each time and then calculate the center of gravity. I want to save these results into an array and then output it using the DAQmx write.


Why not just write the data as you get it?  Then you wouldn't need the array.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 4 of 18
(4,615 Views)

Thank you! I'll try it!

0 Kudos
Message 5 of 18
(4,595 Views)

I added the DAQmx write to the loop but it turned out that it could only output the first data. I need to output the data in each iteration. 

 

Capture.PNG

0 Kudos
Message 6 of 18
(4,582 Views)

You  need to initialize and start the AO before the loop.  Be sure to clear it after the loop.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 7 of 18
(4,575 Views)

I tried to make it correct but I failed...

Attached is my code. Could you help debug it?

 

Thanks!

0 Kudos
Message 8 of 18
(4,571 Views)

@chen3477 wrote:

I tried to make it correct but I failed...

Attached is my code. Could you help debug it?

 

Thanks!


Reread Crossrulz' message:

 

"You need to initialize and start the AO before the loop. Be sure to clear it after the loop."

0 Kudos
Message 9 of 18
(4,566 Views)

I did move it out of the while loop but it didn't work...

0 Kudos
Message 10 of 18
(4,563 Views)