02-06-2015 06:29 AM
Idiom for initialising a sub VI?
I want to have some code which is run only one time at the first call to a sub VI.
My idea is to run a for loop one time (N = 1) at the first call and then zero time (N = 0). An example is shown in the attached code snippet.
When I am using a for loop with a loop tunnel, there is for me an unexpected result. The loop tunnel is NOT keeping its last value. See the attached picture.
My questions are:
1) Is it a better way of doing this?
2) What are the disadvantages with this idiom?
3) Where is it documented that an output loop tunnel is reinitialised to a default value when the for loop starts to execute?
Best regards
Bo Johansson
Solved! Go to Solution.
02-06-2015 06:36 AM - edited 02-06-2015 06:43 AM
Hi bojinlund,
when something is unexpected you should use highlight debugging to understand what's going on…
1) Yes, definitely. When code should run at a certain condition the case structure is the natural choice instead of fiddling with FOR loops…
2) Disadvantage: your unexpected outputs. The FOR loop will/must have an output regardless of their loop iteration count!
3) In the DATAFLOW.
02-06-2015 06:41 AM
02-06-2015 07:10 AM
Hej Jim!
Thank you for the answer!
You need a While loop, a Case structure and you need to handle the “false case”. I need a For loop, two constants and a Select function.
/Bosse
02-06-2015 07:12 AM - edited 02-06-2015 07:14 AM
Hi Bosse,
you need 3 loops and two select functions to produce the wrong result.
Using just one loop with one case structure will give the correct result…
And Jim tried to show you a very similar use case, but it seems you didn't understand it.
02-06-2015 07:57 AM
@bojinlund wrote:
Hej Jim!
Thank you for the answer!
You need a While loop, a Case structure and you need to handle the “false case”. I need a For loop, two constants and a Select function.
/Bosse
Queue the links to beginner tutorials...
/Bosse. You completely missed my point. It wasn't my intention to write your sub-VI initialization code for you, but to show you how to do it by giving a very general example.
You need put the While loop/Case structure/First Call? construct in your sub-VI and it'll give you the same 2D array every time through. The first time it'll generate the array, the rest of the times it'll pull it from the shift register. Your code, however, is so simple that, unless you're doing something really, really quickly, I wouldn't worry about regenerating it every time through.
02-06-2015 09:11 AM
Hej Jim!
Thanks again! Sorry for being unclear! I have mixed two things in my question.
1) I am trying to understand the difference in “my” and “the common” way to initialise a sub VI.
The attached files shows the two ways I am comparing. I have used your “initialization code” in both.
2) I am quite new to LabVIEW and I expected a for loop tunnel to keep its last value.
Where is it documented that an output loop tunnel is reinitialised to a default value when the for loop starts to execute?
/Bosse
02-06-2015 09:15 AM - edited 02-06-2015 09:15 AM
Hej Bosse,
2) Why do you expect something different from DATAFLOW? A tunnel will not keep any previous values - a shift register may keep values…
A tunnel is NOT reinitialized to default when the loop starts.
But it is set to default when a (FOR) loop runs ZERO times! (THINK DATAFLOW!)
02-06-2015 09:26 AM
Hej GerdW!
Have you any reference to this in the documentation of LabVIEW?
/Bosse
02-06-2015 09:28 AM