LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Pass Data outside For Loop

Hi 

 

How do we pass a particular data outside the 'For loop' after one iteration ? I would like to access the value of the tunnel after first iteration and not after the 'For loop' runs out completely.

 

Thanks 

0 Kudos
Message 1 of 9
(4,704 Views)

Define what you mean by "access".  Is the output tunnel set for "autoindexing" or last "value"?

 

It would be easier for us if you would show us your code, but it seems you simply need to start with a few beginner tutorials and learn about dataflow. Please explain in detail what you are trying to achieve, most likley there is a simple solution.

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

The fact that you want to pass a value out of the loop before the loop has finished suggests that you want to use that value elsewhere in your program while the loop is still running - is that correct? You can set the value of a local or global or functional global variable from within the loop and access it from elsewhere but you will have to be very careful to avoid race conditions. Better option is to use a notifier.

 

In the part of your code that uses the data, you need to wait until the value has been set. But how do you know when the value has been set? The notifier node waits until the value has been set, then gives you the new value.

 

Notifiers are in the Synchronisation>Notifier Operations and the help text gives plenty of information and examples.

 

If onthe other hand you just want a value from the first iteration to use after the loop has finished, use a conditional tunnel (right click tunnel and select 'Last Value' and 'Conditonal')

 

conditional tunnel.png

0 Kudos
Message 3 of 9
(4,673 Views)

You can always use a Queue ...

 

Bob Schor

0 Kudos
Message 4 of 9
(4,633 Views)

@Stuart.Parkinson wrote:

The fact that you want to pass a value out of the loop before the loop has finished suggests that you want to use that value elsewhere in your program while the loop is still running - is that correct? You can set the value of a local or global or functional global variable from within the loop and access it from elsewhere but you will have to be very careful to avoid race conditions. Better option is to use a notifier.


You should never give this advice.  Using globals/locals here is beyond terrible.  It's not so much a race condition you'll be worrying about in this case.  Race conditions suggest multiple writers racing to write the value.  The issue is the variable isn't always in sync with the reader.  It may read the same value multiple times or miss values entirely.  When the poster clearly doesn't understand data flow, the single worst thing you can do is give advice like this. Please be more careful. 

 

Beyond that, you should take a look at the difference between a queue and a notifier.  You're using them incorrectly if you're using them how you suggest here.  The purpose of a notifier is to send a single value to multiple places.  This can also be lossy if the writer is sending notifications faster than they are being read.  That's not what the poster would want.  A queue creates a buffer that saves all the values and is read by a single reader.  There are three solutions the poster should consider:

1) output their data as an array and process it after the loop

2) use a queue to send the value to a parallel loop for processing

3) move the processing logic into the for loop

0 Kudos
Message 5 of 9
(4,608 Views)

You should never give this advice.

 

Advice on how not to do it...

 

The phrases "have to be very careful" and "But how do you know when the value has been set?" were intended as a warning, not advice, and I am sorry if that intent did not communicate well.

 

A notifier. Written in one place, read by many. A queue. Writable in many places, read by one. The application determines the correct one to use (and I don't know the application). My answer was incomplete for omitting the queue, not incorrect for suggesting a notifier. From the LV documentation, "Use the Notifier Operations functions to suspend the execution of a block diagram until you receive data from another section of the block diagram..." In response to the question, "But how do you know when the value has been set?" it is a perfectly legitimate answer.

 

I understand if you would like to add further information that you feel would be useful to the discussion, but no need to stamp so heartily on others that are trying to do the same.

0 Kudos
Message 6 of 9
(4,583 Views)

@natasftw wrote:
...  This can also be lossy if the writer is sending notifications faster than they are being read.  That's not what the poster would want. ...

Actually, the poster wants to send a single value, once, after the first iteration of the loop completes. I don't think 'sending notifications faster than they are being read' comes into it.

0 Kudos
Message 7 of 9
(4,561 Views)

If your use case is that you want to update an indicator inside the loop, simply access it inside the loop:

progress.png

 

Thanks,

Danielle

"Wisdom comes from experience. Experience is often a result of lack of wisdom.”
― Terry Pratchett
0 Kudos
Message 8 of 9
(4,559 Views)

There is obviously a lot of confusion about what is being asked.  So before we go too far here, we need to wait for adarshn to come back an clarify their requirements.


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 9 of 9
(4,527 Views)