LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

save data after the for loop

Hi:

I met a problem with saving data in labview.

I am writing this code to save the measurement data from a gauss meter every 3 seconds. The measurement is inside a for loop. I use the "write to spreadsheet" vi to save the data and it works fine.

The only problem is that sometime I want to stop the program earlier but the program wont save the data. I have to manually copy the array to a new vi to save it. I was wondering how to edit the program so that when I hit a "save" button it will save the data...

Thank you very much

Please take a look at my vi.

 

0 Kudos
Message 1 of 11
(6,295 Views)

Have you try converting the for loop to a while loop? i think you can simply right click on the while loop and "replace with while loop"

the for loop will go on until its finish with the loop, and the while loop will stop when you press stop. then it goes to the saving part of your code.

 

 

untitled.PNG

Message Edited by krispiekream on 03-30-2010 12:23 PM
Best regards,
Krispiekream
0 Kudos
Message 2 of 11
(6,278 Views)

Since you're using a FOR loop, the only way to stop your code is by pressing the "abort" button at the top of the screen.  When you do that, your program quits immediately.  Therefore, the "Write to spreadsheet file" vi never runs under those circumstances.  That's why your data isn't being recorded.

 

I can't remember who originally said this, but I think it's appropriate:  "Using the 'abort' button to stop your VI is like using a tree to stop your car.  It works, but there may be consequences."  In other words, it is NOT good practice, and you shouldn't do it.

 

So.  Replace your FOR loop with a while loop.  Create a "stop" button.  Compare the iteration terminal (decremented by 1, since the iteration terminal starts at 0) to the number of times your loop is supposed to iterate.  OR that comparison with the "stop" button.

 

See attached.

 

I've cleaned up your block diagram somewhat as well....your wiring is a mess.  It may not seem important, but it is.  It's very important from a readability standpoint.

 

Hope this helps...

d

0 Kudos
Message 3 of 11
(6,274 Views)

I can't Diane S. vi because I dont have Labview 2009 installed right now.

But the old vi that you made seems to collect data and store it in an array.

but if the program is running for a couple of days, you might run out of memory to store into the array.

so i think you can do this?

save to the file as you go along.

 

untitled.PNG

Best regards,
Krispiekream
0 Kudos
Message 4 of 11
(6,270 Views)

Hello,

 

I could see in the code, that you have shift register for the 2D array which is of no use, you have not used to store all the values. THis 2D array you are using to write into spreadsheet, I believe your spreadsheet stores only the last data and not the entire data,

 

There is no way to stop For loop unless using abort button (exceptions from LV 8.5), as said by others replace that by While loop and use stop button to stop program when needed.

 

 

0 Kudos
Message 5 of 11
(6,263 Views)
I would be careful opening/writing data/and closing the file every loop iteration. This will impact your performance. File access can be very slow, especially if the file is getting very large. If you embed the file write in the loop I would buffer the data for some period of time and then write the data periodically. The best approach though would be to separate the data collection/analysis from the data storage. You can accomplish this using a parallel loop and use a queue to pass the data to be written from the data acquisition task to the one that will write the data to file.


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 6 of 11
(6,261 Views)

Cancancanopen, if you just replace the shift register with a tunnel, you will see that all of the data is getting recorded.  (you are quite right that the shift register right there is completely pointless and should be replaced with a tunnel -- good catch).  The new data is actually being added via the other three shift registers, and then those arrays are being built into a 2D array.  So the 2D array does contain all of the data.

 

The OP does calculate how many times he wants the loop to run, so the array size is bounded (krispiekream eliminated that part of the code in his modification -- it is still present in mine, and my while loop will automatically terminate after the correct number of data points has been obtained).  It was originally a FOR loop, remember?  That being the case, from a memory standpoint it would make better sense to preallocate the arrays and then use "replace array subset" to fill them in with each loop iteration.

 

I agree with Mark that you want to be careful about having your file write and your data collection in the same loop, especially if you are opening and closing the file every time you write to it.  He is of course correct that the right way to do this is using parallel loops and a queue.

0 Kudos
Message 7 of 11
(6,251 Views)

Yeah, you guys are right about this "You can accomplish this using a parallel loop and use a queue to pass the data to be written from the data acquisition task to the one that will write the data to file."

I am still learning labview so I would love to see it in a block diagram?

I dont know how to use a queue yet, but I want to learn.

 

Best regards,
Krispiekream
0 Kudos
Message 8 of 11
(6,238 Views)

Hello Diane thanks, I overlooked the array building.

 

Rest of the things regarding memory consumption, I am in agreement with you guys.

0 Kudos
Message 9 of 11
(6,235 Views)
Krispiekream, there are a zillion queue examples floating around out there...look at the examples that ship with LabVIEW, or do a search on the forum.  Smiley Happy
0 Kudos
Message 10 of 11
(6,209 Views)