LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing output value through for loop

Solved!
Go to solution

How do I pass an output value through the For loop, and have it passing it out while updating (rather than just the final value)? 

0 Kudos
Message 1 of 13
(5,313 Views)

What do you want to do with the data?  Are you passing it to another VI?  Do you just want to display it?  Please be more specific because there are multiple ways to accomplish this (user events, queues, FGV, local variables Smiley Surprised, etc).

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 2 of 13
(5,296 Views)

In general, nothing comes out of a loop until the loop is done.  If you want each of the values to be accumulated and passed out in an array, make the output tunnel an Indexing tunnel.

 

In LabVIEW 2016, if you have parallel loops, you can pass out individual elements using Channels.  Note, however, that the Channel needs to go to an independently-running loop -- if it is "connected" in any way (for example, by "eating" the Error Line coming from the For Loop), the second loop (because of Data Flow) can't run until the first loop exits, anyway.

 

The "traditional" (i.e. pre-LabVIEW 2016) way of violating Data Flow and "exporting" values as they arose in loops was via a Queue, the so-called Producer/Consumer Design Pattern.  You can find examples by opening a blank VI, going to the File menu, choosing New ... (the dots are important), and finding "From Templates", "Producer/Consumer Design Pattern".

 

Bob Schor

0 Kudos
Message 3 of 13
(5,269 Views)

I always have this link ready: Producer/Consumer


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 13
(5,231 Views)

I've tried to implement it using Producer/Consumer example, not sure if I'm approaching it correctly. I'm trying to read from a file (also attached), and read each individual value. Please let me know if I'm implementing it correctly, I'm not getting the desired results. 

Download All
0 Kudos
Message 5 of 13
(5,156 Views)

In your "producer" loop only 1 element would ever get enqueued.  Try replacing your while loop with a for loop and use indexing.  That way each loop iteration you will send a single 1D array until you run out of data.

Message 6 of 13
(5,150 Views)
Solution
Accepted by topic author AndreaD

Andrea,

     What are you trying to do, and when are you trying to do it?  [Echoes of Watergate ...]

 

     You have a file that we've discovered can be read as a binary file having "records" that are 1651 bytes long.  You have a number of options open to you on how to process this, with "Time" and "Space" being variables in the equation.

  1. Read the entire file into memory.  Use Reshape Array to make an array of N "Records".  Process the Array one record at a time by passing it into a For Loop using an Indexing (default) tunnel.  Requires the most Space, Time moves sequentially.
  2. In a While Loop, read a record (1651 bytes) into memory.  Process the record.  Stop when you reach End of File.  Requires minimal Space, Time still moves sequentially.
  3. Use Producer/Consumer.  Producer Loop reads a record and puts it on Queue, continuing until EOF, at which point it puts a Sentinel (an empty record) on the Queue and exits, leaving the Queue intact.  The Consumer, running in parallel, waits for a Record, then processes it.  If it was the Sentinel, the Consumer exits and Releases the Queue.  Space is intermediate (depends on relative speeds of Producer and Consumer), Time is used optimally (Producer fills gaps when Consumer is "busy", Consumer fills gaps when Producer is "busy").

I don't know why you would need one method or another, as the nature of the processing isn't clear (to me -- you may have described it earlier and I've just forgotten).  I tend to use P/C when I have a Producer that spends a significant amount of time "waiting for data" (such as an A/D converter producing 1000 samples at 1KHz continuously -- it is very busy, but spends 999 milliseconds "waiting for data to be available" and 1 millisecond giving me the data, so with P/C, I spend those 999 milliseconds processing the data I just received, keeping the CPU busy.

 

To help illustrate one way of using P/C with this project, here's one way to use Queues to process records, as in Step 3, above.  I realize you are using LabVIEW 2013, and thus can't use these directly as Snippets, but I'm hoping that they are simple enough you could create them "from scratch".  I've also included the other cases for the two Case Statements as they contain "important" code, namely the Sentinel code.  Note also that arranging the parallel loops in a vertical column gives a subtle visual impression that they happen simultaneously, rather than sequentially.

Record Queue 1.pngRecord Queue 2.pngRecord Queue 3.png

 

Bob Schor

 

0 Kudos
Message 7 of 13
(5,118 Views)
Solution
Accepted by topic author AndreaD

I'm doing this because I need to read the data like it's streaming in like data coming in. I know I'll need to put a timing method to control the speed of data coming in as well. 

0 Kudos
Message 8 of 13
(5,084 Views)

OK, if you take my code and put a Wait function, say with 1000 wired to it, you will read one record a second and pass it on to the Consumer for processing.

 

But why do you want to do it this way?  Are you trying to simulate something that spits out 1651-byte records at regular intervals?  Where does the need for parallelism come in?  If we call the code "inside" the Producer loop "Read A Record.vi" and the code inside the Consumer loop "Process A Record.vi", what's wrong with a For Loop containing Read A Record directly connected to Process A Record?  True, you are doing these two operations in series rather than in parallel, but I don't see a compelling reason to add the extra code to create a parallel design.  [Of course, I don't see your Big Picture ...].

 

Bob Schor

0 Kudos
Message 9 of 13
(5,068 Views)

I may not be setting this up correctly, but I'm trying to simulate reading a set of data like it's streaming. I'm trying to read through a data file 

0 Kudos
Message 10 of 13
(5,039 Views)