LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

shift register without initialization get different results

I wrote a simple program using for loop with un-initialized shift register, for 1D and scalar data, it gave different results. It was run in LabVIEW 7.0. Could anyone test it and find out the problem? Is that a bug of LabVIEW?
 
The file is attached.
 
Scalar one: it is running as expected.
 
1D arrary, does not work at all!
 
thanks.
 
0 Kudos
Message 1 of 11
(4,212 Views)

Hi Tony,

This is expected...  It is not a bug.

You have to initialize the shift register before the loop, especially, if you do not force an initialization within the loop.  Otherwise, you will not be able to predict the outcome.  Furthermore, if you create a sub-vi which is called more than once, it will retain the last value as the starting point, which is unlikely to be the behaviour that you would want.

Simply initialize the shift register to the left of the loop.  A typical method is to wire a constant of 0 (or array of zeros).

RayR

Message 2 of 11
(4,207 Views)
thanks for the quick reply, but my question is not the initialization problem. I want to preserve the results so I do not want to initialize the shift register.
 
If you open the attached VI, you will find out that the same block diagram for a For Loop, the results of shift register are different for a scalar variable and a 1D array variable. Actually, the output of the 1D array does not change at all.
 
thanks.
 
tony
 
0 Kudos
Message 3 of 11
(4,187 Views)
If you don't initialize a shift register, it defaults to the default value for the data type of the shift register.  This means that your array shift register is defaulting to an empty array.  An empty array added to a non-empty array will always return an empty array.
 
Even if you need to preserve the data, you still need to initialize it on the first run of the VI - you just need to do it inside the loop.
 
Check out the attached VI.
 
Jaegen
0 Kudos
Message 4 of 11
(4,183 Views)

I like what Jaegen proposed. 

If it's the first time that the vi runs, it will do the initialization.

However, you mentionned this: " I want to preserve the results so I do not want to initialize the shift register. "

You'll have to define that behaviour.  Do you mean each time it is called, you want it to use the last values?  However, you would want to have the initial values to be predictable, right?  And what about if the vi is closed & re-opened or something obscure occurred in between (not that it ever happens, right? 😉 )

You may want to read an ini file that contains the "first-run values" and could / would store the last values for each time the sub-vi (or vi?) is called.

The important thing is to have a predictable / reproduce-able behaviour.  ... and expected results...
 
RayR
0 Kudos
Message 5 of 11
(4,176 Views)
thanks a lot! That solves the problem.
 
The interesting thing is I did not expect to get empty for an ADD function. I think for an ADD function, logically, something ADD nothing = something, not nothing makes sense.
 
thanks again. 
 
tony
0 Kudos
Message 6 of 11
(4,172 Views)


@TonyFeng wrote:
The interesting thing is I did not expect to get empty for an ADD function. I think for an ADD function, logically, something ADD nothing = something, not nothing makes sense.


Actually, when you do not initialize the shift register, it's contents are not "nothing" it is whatever residue is in that memory location.  If you're lucky the value is zero (0), so "something" + 0 = "something", eg: 6 + 0 = 6.

Let's say "something" = 6:   

If the residue in memory was 623458723647823746827364273655 + 6 = 623458723647823746827364273661, which is an unexpected result. 

I'm illustrating this to show that you are not adding blank, thus resulting in blank...  There is a value in memory.  Which is why it is always important to initialize values..

RayR

0 Kudos
Message 7 of 11
(4,139 Views)

We seem to need to clarify things ...

If I open a new blank VI, and create a for loop with an array constant wired to its right-hand-side but nothing wired to its left-hand-side, when I first run it the value out of the left-hand-side of the shift register will always be an empty array.  And if I close and re-open the VI (as long as it's not somehow retained in memory on the clip board Smiley Wink ), the first time I run it, the left-hand-side will always be empty again.  Also, if the operations I do on the array do nothing to overcome the fact that it's empty (like only adding it to another array), and therefore an empty array always gets written to the right-hand-side of the shift register, the left-hand-side will always be empty.

However, if the shift register is uninitialized, but somehow a non-empty array gets written to the right-hand-side, then between runs of the VI while it's held in memory (either separate clicks of the "run" button, or separate runs of the VI as a sub-VI), the shift register will retain whatever value got written to its right-hand-side.  This means that the data may persist from one run of your program to another, which may not be desired.

Hopefully this isn't too muddy ...

Jaegen

 

0 Kudos
Message 8 of 11
(4,120 Views)

Jaegen,

I'm reading these posts at lightening speed during short breaks  😉

You got it!

When you close a vi and re-open it.  The FIRST time you run the vi, the non-initialized array or value for a shift register will initialize itself to the default value for the given type.

The NEXT time you run it, it will (may?) initialize itself to whatever value remained in memory (the residue in memory that I mentionned earlier). 

Uninitialized shift registers are not a good idea..  🙂

RayR

0 Kudos
Message 9 of 11
(4,108 Views)


@JoeLabView wrote:

 ... Uninitialized shift registers are not a good idea..  🙂


Unless you initialize them inside the loop of course.
Message 10 of 11
(4,105 Views)