From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to stop code from overwritting elements in the array?

Solved!
Go to solution

Hello everyone,

im pretty new to labview and im trying to code a tool that is analyzing the duration of a signal. The values im getting with the waveform duration vi is displayed in a histotgramm graph. The thing is, the code should run for a while and when its gonna acumulate a lot of data, the anomalies are going to get lost in the histogramm. The Signal is just a simulation im working with for the later experiment in the laboratory

 

My idea was that im writing a case-structure where im defining default value +/- tolerance. I want to write the values that are out of tolerance into an array and display it into a graph.

The code works and the values are getting written into the array, but it always overwrites the elements in the array.... Im sitting for hours at the problem now and i cant solve it.....

 

Does anybody have a solution or an idea why its not working? Thanks a lot to the people taking their time to help a noob in labview!

I have attached the part of the code that im have trouble with.

 

Best Regards!

 

P.S. Sorry if my english is not that good, not a native speaker here

0 Kudos
Message 1 of 7
(1,399 Views)
Solution
Accepted by topic author Flouran93

Why are you placing your values into the array inside a for loop? Your for loop will run for as many iterations are your "i" value is from the while loop. For the first iteration of the loop, you will NEVER place any values into the array since you are wiring a 0 to the "n" value of the for loop. If your condition is true for the second iteration, you will put one value into the array. If the third iteration of the while loop meets the condition, you will write two values in the array, starting at index 0. If your condition is met in the 1000th iteration of the while loop, you will write 999 entries of the same value into your array, again starting at index 0 thus overwriting any previous data in the array.

 

What would seem to make more sense would be to write either a 2-D array so that you can write the current loop iteration your condition was met as well as the value at that point in time. Or you could have an array of clusters. The cluster would contain the iteration value when the condition was detected and the second element would be the value.

 

You can use a conditional, auto indexing terminal on your while loop to build the array of values.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 2 of 7
(1,369 Views)

Hello Mark,

thanks for answering to my post.

i dont fully understand clusters yet, so i didnt try anything with that method. i was just using the for-loop to fill the array because thats what i saw in the basic videos on youtube.

 

I tried the method with the conditional auto-indexing out of the while-loop and it worked. Is there a possibility to build the array inside the while-loop? The Methode with the auto-indexing out of the while-loop works, but it shows the results after the while-loop stops, that means that i cant see the data that is going to be displayed into the graph until the data aquisition is completly done.

 

Thanks a lot for trying to help me and sorry if im asking stupid questions without realizing

0 Kudos
Message 3 of 7
(1,352 Views)

You can use shift registers on your while loop and a "build array" node to build the array inside the loop.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 4 of 7
(1,326 Views)

@Flouran93 wrote:

I tried the method with the conditional auto-indexing out of the while-loop and it worked. Is there a possibility to build the array inside the while-loop? The Methode with the auto-indexing out of the while-loop works, but it shows the results after the while-loop stops, that means that i cant see the data that is going to be displayed into the graph until the data aquisition is completly done.

 


Several additional comments:

 

  • You are not simulating acquisition timing, so the while loop runs as fast as the computer allows. If the boolean is mostly true, you'll quickly run out of memory, because the data in the output tunnel grows without limits.
  • It is convention to have wires flowing left-to right and have output tunnels on the right. Having it on the left is very bad form.
  • Why do you have so much duplicate code? (e.g. one 1e-6 constant is sufficient. You can branch the wire).
  • There is a primitive for 1/x. (you could even combine the division and multiplication)
  • Maybe the "in range" function could simplify some of your code.
  • Use reasonable labels for readability. "array 2" is not a useful label.
  • I recommend to relatively quickly get away from dynamic data and express VIs. There are typically better and more efficient solutions.
  • Yes, building the array in an initialized  shift register is the right way. (If you know the final size, it would be more efficient to initialize at full size, the replacing elements as you go. This avoids expensive memory reallocations during the run.)
  • Save the VI with reasonable defaults in all controls. zero is not a useful "duration" because it will result in a NaN due to a division by zero..
Message 5 of 7
(1,269 Views)

Here are some of the points I mentioned illustrated. Still not perfect but should give you some ideas....

(Note that is a literal translation and does not judge the validity of the algorithms ;))

 

altenbach_0-1611163539544.png

 

Message 6 of 7
(1,255 Views)

Hello altenbach,

thanks a lot for your comments and tips. It really helps me as a newbie learning to work with labview.

You're completly right that the code i wrote can be considering as poor coding, but my primary goal is trying to set it up, so its runing.

 

Also thanks a lot for the modified example you attached in your post.

 

Have a great day!

 

0 Kudos
Message 7 of 7
(1,227 Views)