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 output last 4 arrays from a For Loop?

Solved!
Go to solution

Hi People,

I am almost at the end of my tether. I really hope someone could please help me.
Any input would be welcome. VI attached.

 

VI explanation:
I initialize an array.
The random number generator simulates my camera input.
Depending on which iteration it is, the data is added into one of four output arrays.
'x-y*floor(x/y)' gives a remainder value of 0, 1, 2 or 3.
'Case Selector' just adds 1 to 'x-y*floor(x/y)' to get case 1, 2, 3 or 4.
Shots 1, 5, 9 etc are added to Array 1 (Case 1).
Shots 2, 6, 10 etc are added to Array 2 (Case 2).
Shots 3, 7, 11 etc are added to Array 3 (Case 3).
Shots 4, 8, 12 etc are added to Array 4 (Case 4).
Averaged Output displays the averaged value i.e. the total value divided by the number of shots saved in that array ('Set (IQ+1)').

 

My problems are:

1) I would like to output only the last 4 sets of data from the Averaged Output 1, 2, 3 and 4 to 4 different files (i.e. save as .csv what is displayed onscreen in indicators 'Averaged Output 1', 2, 3 and 4 at the end of all iterations.
Where should I place my file save diagram disable so that it does this?

 

Putting it outside the main For Loop with auto-indexing on gives me one file with all previous data. (This is not feasible as my number of shots should number in the thousands)

Putting it outside the main For Loop with auto-indexing off gives me only the last set of data. (I need the output for 4 Arrays, not just the last run)

Putting it inside the main For Loop (as shown) gives me the same number of files as number of iterations. (Again not feasible due to large number of files that will be generated and slow camera capture)

 

For the sake of fast camera capture, I would like these 4 files to only output once all the image captures are complete.

 

2) Would preferably like to name the file once and for the programme to append '(1)', '(2)', '(3)' and '(4)' to filename of the appropriate 'Averaged Output' Arrays but file path controls are another big headache for me.

 

3) P.s. is initializing one array enough to avoid using Memory Manager? Or should I initialize 4 arrays?

 

I am using Labview 2010.

Thanks so much,
Jaslyn

0 Kudos
Message 1 of 9
(2,981 Views)

Hi,

 

First, what do you mean by "add"? Your code in your case strcuture sums your input values, so your array size doesn't change. However, you also said "Putting it outside the main For Loop with auto-indexing on gives me one file with all previous data. (This is not feasible as my number of shots should number in the thousands)" -- it sounds like you are expecting an array with thousands of elements. So, what should "Output Array N" look like?

 

(Note: Most of your arrays are 4x4, but in Case #2 you created 128x128. I prersume this is a typo?)

 

 

Second, why do you add your "simulated camera input" to the "Initialized Array"? You can add it direcly to the previous output inside your case structure.

 

Third, have a look at shift registers: http://www.ni.com/gettingstarted/labviewbasics/shiftregisters.htm. Use them instead of Feedback Nodes to make your code tidier.

 


@jaslyn wrote:

 

1) I would like to output only the last 4 sets of data from the Averaged Output 1, 2, 3 and 4 to 4 different files (i.e. save as .csv what is displayed onscreen in indicators 'Averaged Output 1', 2, 3 and 4 at the end of all iterations.
Where should I place my file save diagram disable so that it does this?

 

For the sake of fast camera capture, I would like these 4 files to only output once all the image captures are complete.


You will need to call "Write to Text File.vi" 4 times to write 4 files. So, you should finish your loop, then call this VI 4 times.

 


@jaslyn wrote:

2) Would preferably like to name the file once and for the programme to append '(1)', '(2)', '(3)' and '(4)' to filename of the appropriate 'Averaged Output' Arrays but file path controls are another big headache for me.


 You can create strings first, then convert them into paths: http://zone.ni.com/reference/en-XX/help/371361G-01/glang/string_to_path/

 


@jaslyn wrote:

3) P.s. is initializing one array enough to avoid using Memory Manager? Or should I initialize 4 arrays?


I'm not sure what you're asking; can you please clarify what you mean by "avoid using Memory Manager"? But anyway, you've actually initialized FIVE arrays: 1 outside the loop, and 1 inside each case.

Certified LabVIEW Developer
0 Kudos
Message 2 of 9
(2,953 Views)
Solution
Accepted by topic author jaslyn

Hi jaslyn

 

I presume that you want to store the 4x4 array of average output for the last 4 iterations (correct me if I'm wrong). If so, I have set up the example as shown below:
Untitled new.jpg

 

So basically, what I have done is that I have created a comparator to check if the iteration is of the last 4 total iteration you have set. I have included a number at each of the case structures to define which case it belongs to. The comparator will pass a boolean output to the case structure which contains the algorithm to writing a file. Since you mentioned that you want to create a file for each of the case, I have created the build path function to create custom file name for each case. You'll notice that there is a concanate string.vi which allows me to customize the name of the file. Since the common file is to be Average Output, I take the numeric constant taken from the case structure and convert it to string and concanate it to make it into e.g. Average Output 1.txt. Please note that you'll need to put a base path such as for my case, I would like to save it in the folder named 'New folder(3)' at my desktop. 

 

The write to file case only occurs if the condition is true (i.e. only the last 4 iterations of the for loop)

 

Once done, I have created the header and write it into that text file and followed by the spreadsheet data on the next line.

 

I have attached the code for your reference. Let me know if this is what you wanted.

 

 

Thank you

 

Warmest regards,

Lennard.C

Learning new things everyday...
Message 3 of 9
(2,949 Views)

Dear Lennard,

 

Ohmygosh thank you so much!!!! You have by and large solved my problem, thank you!!!!

 

I just have two questions..?
1) Why is the subtraction 4 in every case but case 4 which subtracts 3?
This is to determine if this iteration is one of the last 4 iterations, right? Why do you subtract 3 and not 4 in case 4?


2) What's in the Diagram Disable structure and why is it only disabled for case 1? (array size, index array, subtract 4, delete from array) What is it trying to do?

I blocked it out in one of the cases using DIagram Disable but it made no difference to the data.

 

Thank you so much, Lennard!!!

Jaslyn

0 Kudos
Message 4 of 9
(2,921 Views)

Hi jaslyn,

 

You're welcome. 😉

 

About your question:

  1. I'm terribly sorry for my mistake. You'll need to subtract 4 in every case therefore just do the necessary changesSmiley Wink. Kinda overlooked the case 4. Yes, I subtract 4 to determine if it is of the last 4 iterations. 
  2. Actually, I was trying to find a workaround to your problem. You can just ignore it. What it does is that I'm taking the size of your Average Output and take the 4th row of your data. But I doubt that is what you wanted since you mentioned you want the set of it so I disabled it. 

Anyways, I have attached the modified version of that file based on your feedback. 

 

Thank you 

 

Warmest regards,

Lennard.C

Learning new things everyday...
Message 5 of 9
(2,895 Views)

Thanks so much Lennard!!

 

Hi JKSH,

I've been busy trying to complete the dang code, but for completeness so that other users can learn too, I shall answer your queries. 🙂 Thanks for looking in.


First, what do you mean by "add"? Your code in your case strcuture sums your input values, so your array size doesn't change. However, you also said "Putting it outside the main For Loop with auto-indexing on gives me one file with all previous data. (This is not feasible as my number of shots should number in the thousands)" -- it sounds like you are expecting an array with thousands of elements. So, what should "Output Array N" look like?

Ans: Output Array N should be a 4x4 array of the summation of all the numbers generated during every 4th iteration.
i.e. if random number 1=1, random number 2=2, etc, And 'No of iterations' is 12,
Output Array 1 should be 1+5+9:
15 15 15 15
15 15 15 15
15 15 15 15
15 15 15 15

(Note: Most of your arrays are 4x4, but in Case #2 you created 128x128. I prersume this is a typo?)
Ans: Kind of, I'm using a 4x4 array of randomly generated numbers as an example but my actual data will be the 128x128 pixel output of a camera. And during actual experimentation, 'No of Iterations' will number in the thousands.


Second, why do you add your "simulated camera input" to the "Initialized Array"? You can add it direcly to the previous output inside your case structure.

Ans: See answer to question 3

 

Third, have a look at shift registers: http://www.ni.com/gettingstarted/labviewbasics/shiftregisters.htm. Use them instead of Feedback Nodes to make your code tidier.
Ans: I did try.. But I can't seem to add shift registers to the case structure, only the for loops outside. Are you sure it can be done..?


jaslyn wrote:

1) I would like to output only the last 4 sets of data from the Averaged Output 1, 2, 3 and 4 to 4 different files (i.e. save as .csv what is displayed onscreen in indicators 'Averaged Output 1', 2, 3 and 4 at the end of all iterations.
Where should I place my file save diagram disable so that it does this?

For the sake of fast camera capture, I would like these 4 files to only output once all the image captures are complete.
You will need to call "Write to Text File.vi" 4 times to write 4 files. So, you should finish your loop, then call this VI 4 times.

Ans: How do I get it to extract the data from each of the 4 cases in my case structure?

jaslyn wrote:
2) Would preferably like to name the file once and for the programme to append '(1)', '(2)', '(3)' and '(4)' to filename of the appropriate 'Averaged Output' Arrays but file path controls are another big headache for me.
You can create strings first, then convert them into paths: http://zone.ni.com/reference/en-XX/help/371361G-01/glang/string_to_path/
Thanks


jaslyn wrote:
3) P.s. is initializing one array enough to avoid using Memory Manager? Or should I initialize 4 arrays?
I'm not sure what you're asking; can you please clarify what you mean by "avoid using Memory Manager"? But anyway, you've actually initialized FIVE arrays: 1 outside the loop, and 1 inside each case.

I've read that to avoid fluctuations in memory usage, it is a good idea to initialize arrays to their expected size before the start of data collection. That was just what I was trying to do.

0 Kudos
Message 6 of 9
(2,815 Views)

Hi Guys,

 

Is it possible to store, for example tick counts or image pixel data, in an array first, then save it to a file AFTER the programme is done running (or at least when the instruments have done their work)?

The reason for this is so as not to slow the programme down by taking up resources saving as it is running.

 

Any thoughts? Thanks lots.

 

Best Regards,

Jaslyn

0 Kudos
Message 7 of 9
(2,685 Views)

jaslyn wrote:

Is it possible to store, for example tick counts or image pixel data, in an array first, then save it to a file AFTER the programme is done running (or at least when the instruments have done their work)?

The reason for this is so as not to slow the programme down by taking up resources saving as it is running.


Yes, since LabVIEW is a full featured programming environment, anything is possible. You should however start a new question instead of adding to an only remotely related old discussion that is already marked as solved.

 

How much data are you trying to accumulate?

0 Kudos
Message 8 of 9
(2,632 Views)

Hey...

 

I thought I replied to this ages ago... Sorry for the delay.

 

Thanks for the reply, altenbach.. Feels good for a newbie to see a revered guru replying to her thread. 🙂

 

I'll start a new question, but basically, 10000 tick counts and 10000 128x128 pixel images. The images are being output automatically but I wish I could just highlight, copy and paste the tick counts from the array (like in Excel) after the programme is done running.

 

Thanks for replying!! ^^

0 Kudos
Message 9 of 9
(2,554 Views)