LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Possible For loop bug (LV2016)

Solved!
Go to solution

Here is a trick question.

What value will the "Loops" indicator be after the vi executes?

For Loop Bug.PNGA) -1

B) 0

C) 99

D) 100

 

If you guess C, you should be right, but you are wrong! The value is actually zero! Had me scratching my head for awhile.

Has anyone else run across this before?

The work-around is to use a real 2D array.

 

 

Using LabVIEW 2016, version 16.0f2 (32 bit) on 64bit Windows 7

 

---------------------------------------------
Certified LabVIEW Developer (CLD)
There are two ways to tell somebody thanks: Kudos and Marked Solutions
0 Kudos
Message 1 of 25
(3,531 Views)

Seems logical. Take the array size of array 3, it won't be 0,0. 

An array is empty if the product of all dimension sizes is zero. You can have a 2D array with one row and zero columns or zero rows and 1k columns. Both empty.

 

(Sorry, posting by phone, will look at it later)

0 Kudos
Message 2 of 25
(3,524 Views)

Ah.. but that is the problem. Empty array is TRUE. I never thought about getting the size.

You are right.

For Loop Bug After.PNG

We have one row of an empty array. (Very confusing)

But still.... Either "Empty" should be false, or size should be 0,0.

---------------------------------------------
Certified LabVIEW Developer (CLD)
There are two ways to tell somebody thanks: Kudos and Marked Solutions
0 Kudos
Message 3 of 25
(3,512 Views)

Not sure what you are doing to get -1 at the loop iterator, when I run your VI the Loops = 0 and Empty? = True, which does not match your screenshot.

 

Altenbach is dead on, of course, when you build your empty 1D array into a 2D array the size becomes 1 row by 0 columns, so your For Loop will run 1 time.

0 Kudos
Message 4 of 25
(3,506 Views)

Bit it ***is*** empty, i.e contains no elements, even if on of the dimension sizes is nonzero.

0 Kudos
Message 5 of 25
(3,504 Views)

First screenshot the VI has not run (default Loops value set to -1)

Second screenshot was after it ran. Hence the Loops = 0

 

What I am having a problem with is that "non-zero = empty" 

I guess I have a different definition of empty. I always thought that if you had a non-zero amount of marbles in a jar, the jar was not empty. OTOH, if I had 2 jars in a bag, and they were both empty....

I must be loosing my marbles.

---------------------------------------------
Certified LabVIEW Developer (CLD)
There are two ways to tell somebody thanks: Kudos and Marked Solutions
0 Kudos
Message 6 of 25
(3,484 Views)

Is it a bug with the For Loop or the Empty Array Comparison. Smiley Wink

 

Below gives Empty Array True and Loops = 0

 

Snap6.png

The next gives Empty Array True and Loops = 99

Snap8.png

 

Since the For loop is grabbing the first row of a 2D array, I guess it is possible to have an array of empty columns, but not have columns of empty rows. (Not sure what I am saying, need some sleep.)

 

mcduff

 

EDIT: Possible to have an array of empty arrays, it is still empty, but the number of empties is quantified. (Still need some sleep)

 

0 Kudos
Message 7 of 25
(3,482 Views)

@mcduff wrote:

Is it a bug with the For Loop or the Empty Array Comparison. Smiley Wink


Nope.  No bug with either.

 

Empty Array? is TRUE when the product of the array sizes is 0.  With the Build Array, we are setting the number of rows to 1 (number of inputs to the Build Array) and the number of columns is 0.  1*0 = 0, so Empty Array? returns a TRUE.

 

The FOR loop just takes the number of rows (in our case 1) and sets N to be that based on the autoindexing tunnel.  So the loop should iterate 1 time.  This will result in Loops being 0 at the end of the VI running.

 

So everything works per the specification.  Now if you want to add smarts to the autoindexing tunnel to not iterate on an empty array, then add it to the Idea Exchange.


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 8 of 25
(3,475 Views)

What is strange is the specification.

 

Consider 1-d array that is empty. In this case consider it a row vector. I could say it contains infinitely many empty columns. Not so good because a For loop would iterate forever. So a row vector cannot iterate over empty columns.

 

Now consider a 2-d array. I specify it has N empty rows in it, and 0 empty columns. Empty array tells me its empty, I cannot see anything in it, but when I take the array size it returns N, 0. So now I have rows of empty columns, the items in the row cannot iterate in a For loop, but the row that contains nothing can iterate in the For loop. I am not sure how an empty column/row counts as an element.

 

But I see Crossrulz's logic and agree that is the way it works. What I need now is rolfk to explain that on the original LabVIEW on the mac you could have empty arrays of arrays in Pascal, ... Smiley LOL , so that is why it was done.

0 Kudos
Message 9 of 25
(3,456 Views)

This has been beaten to death several times in the past. And there are several discussions with people like Aristos Queue and others from NI chiming in on why a multidimensional array can have one dimension not being 0 and still be empty. I'm sure altenbach and others with magical search capabilities can turn up posts from decenia ago either here or on lavag.org. NI's search engine regularly has me flabbergasted and Google is not so good in turning up very old posts, but altenbach seems to be regularly able to resurface relevant threads from the past.

 

It has nothing to do with the Mac, but simply that resetting all dimensions to 0 whenever any of the dimensions turns 0 has some pretty far reaching consequences that make various things in LabVIEW way more complicated than needed or even impossible.

 

There is nothing strange about an array having N * 0 elements. Mathematically that is a fully valid notation and the way LabVIEW implements multidimensional arrays does not exclude that possibility.

Unexpected for the novice? Yes!

Buggy? Absolutely not!

Rolf Kalbermatter
My Blog
Message 10 of 25
(3,420 Views)