LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Help with Saving Multiple Array Values Without Overwriting in LabVIEW

Solved!
Go to solution

Hello everyone,

I’m currently working on a project in LabVIEW where I have a button that saves the value from my numeric indicators into an array. However, I’m encountering an issue. Each time I press the button, the value gets saved, but it seems to overwrite the previous value, keeping only the most recent one.

What I need is for the array to store each value I save without losing the previously saved values, essentially creating a history of values. I would like some guidance on how to properly append new values to the array instead of replacing the previous ones.

Here’s what I’ve tried so far:

  • I’ve used the "Build Array" function, but it still only keeps the last value.
  • My button is inside a while loop, and I’m using shift registers to store the array, but something isn't working as expected.

Could anyone provide a solution or point me in the right direction? I’d appreciate any help you can offer.

Thanks in advance!

0 Kudos
Message 1 of 17
(734 Views)

You need to start with some basic tutorials, this is completely dysfunctional!

 

  • You have a useless loop inside a greedy loop
  • Your shift register is not initialized and should be blue, not orange (do you know the difference?)
  • For every iteration of the outer loop, the FOR loop repeats the same operation N times.
  • Having these controls inside the FOR loop is pointless because they probably won't change during the execution of the loop.
  • Never ever make a label an empty string. You can hide the label on the front panel but it should always show on the diagram

All you needs is a shift register in the outer while loop. Delete the inner loops. Initialize it with a 2D array and build your controls into a 1D array and append it as new row whenever the button is pressed.

 

See how far you get....

 

If you don't understand the flow of data, try execution highlighting.

Message 2 of 17
(718 Views)

There appear to be multiple things you don't realize about For Loops, the difference between scalars and arrays, between Controls and Indicators, and other fundamental things.

 

Normally, a Control named A that has a single Dbl stored in it can be right-clicked and changed to an Indicator named A that stores a single Dbl.  Similarly, A can also designated a 1D Array of Dbl, but though it is named the same as the "scalar" variable "A", it is different, and naming both a scalar (A) and a 1D Array (A) the same is a Bad Idea and eventually will lead you to a broken wire.

 

When you say "Saving Multiple Array Values without Overwriting", where do you want to save them?  If you save them in memory, the amount of data you can save is governed by the memory of your PC, say 8 GB.  If you save them to disk, the limitation is much higher, often 1 TB or more.

 

Do you want to be able to look at all the values?  Then "in memory" might be what you want.  If they are "results/data" to be looked at later, then "on disk" is probably better.

 

You should never have a Control ("Salvar") in a Block Diagram without a label!  Why hide it?  It can only cause you trouble.  [Oh -- are you trying to make an Event Structure, something that will "Fire" every time you push the "Save" button?  Learn about Event Structures!  Don't re-invent basic LabVIEW tools.  Resist writing code for "Add", for example.

 

Do you understand what the For loop does?  The first time, it builds a bunch of 1D Arrays of size 1, holding the values of the Front Panel controls.  The second time, it overwrites the 1D Arrays of size 1 with (similar) Arrays of size 2.  Note the original data are lost.  The third time, the Arrays of size 2 are overwritten by Arrays of size 3.

 

I bet that is not what you want.  I'm guessing you want the first Array to have the first value(s).  The next time through the (For) Loop, the output should be the previous Array with the new inputs appended at the end.  What do you know about a Structure (most commonly used with While Loops) that have an Input that was formerly the Output of the previous run?  A related question -- how do you restrict a While Loop to only running once?

 

With these hints, you should be able to solve 90% of the issues with your code.

 

Bob Schor

 

Message 3 of 17
(709 Views)
Solution
Accepted by MarcusP.

Here's one possible simple implementation:

 

altenbach_0-1728329486814.png

 

Message 4 of 17
(705 Views)

Since your quantities L, E, DI, DF, CI, CF, etc. appear to be independent measures (even though all of the same type), rather than make an array of these quantities, I'd make a Cluster whose elements are named L, E, DI, DF, CI, CF, etc.  (Hint -- always create a TypeDef from your Cluster and use it everywhere you reference the cluster.  This will save you from writing code that connects CI to DF and DF to CI).

 

Bob Schor

0 Kudos
Message 5 of 17
(686 Views)

Holy, while I was sleeping, they were destroying me—just kidding! So, let me see what I can do:


  •  

    • You have a useless loop inside a greedy loop

    .



    First, I noticed that It was totally useless at that moment. I was completely clueless about how to pass the value I wanted through the loop and create an array that would keep a history of the values (I don’t even know how to set this up!).

    • Your shift register is not initialized and should be blue, not orange (do you know the difference?)

     



    If I say I don’t know, would you be mad? Well, jokes aside, no, I really don’t know the difference. I think it might be about the variable type, like int, float, and so on, like how the wires usually work. But I’ve never looked deeply into it, so I don’t know if that’s what you’re referring to.

    • For every iteration of the outer loop, the FOR loop repeats the same operation N times.

     



    Yeah, I think this might be the problem in my .vi.


    • Having these controls inside the FOR loop is pointless because they probably won't change during the execution of the loop.

     



    The idea was to use this .vi in another application, then connect it to this one, so I could make it like a "function" and repeat it in other parts of the application without having to redo all the connections.


    • Never ever make a label an empty string. You can hide the label on the front panel but it should always show on the diagram

     



    Okay, this was something that Bob pointed out as well, so I guess it could really be a problem. I’ll try to solve this too.

By the way, I saw what you did, and it worked perfectly, just like I wanted! So, really, thank you!

0 Kudos
Message 6 of 17
(653 Views)

Well, I admit that even after using LabVIEW for about 4 months, I still struggle with how to do most of the things I need. That’s why I came to the American forum (even though I'm in Brazil, just saying... it's good here, but you guys usually respond faster—probably because of experience).

 


Normally, a Control named A that has a single Dbl stored in it can be right-clicked and changed to an Indicator named A that stores a single Dbl.  Similarly, A can also designated a 1D Array of Dbl, but though it is named the same as the "scalar" variable "A", it is different, and naming both a scalar (A) and a 1D Array (A) the same is a Bad Idea and eventually will lead you to a broken wire.

 


 

Hmm, I only made it like this because I was referencing this .vi in my application, and the indicator I use is a scalar, but I think I can change that.

 

 

When you say "Saving Multiple Array Values without Overwriting", where do you want to save them?  If you save them in memory, the amount of data you can save is governed by the memory of your PC, say 8 GB.  If you save them to disk, the limitation is much higher, often 1 TB or more.

 

Do you want to be able to look at all the values?  Then "in memory" might be what you want.  If they are "results/data" to be looked at later, then "on disk" is probably better.

 


As for saving the data, it’s simple. I plan to save it as an image of the .vi for demonstration purposes, and then I’ll store it in a folder with the test name.

 


 

You should never have a Control ("Salvar") in a Block Diagram without a label!  Why hide it?  It can only cause you trouble.  [Oh -- are you trying to make an Event Structure, something that will "Fire" every time you push the "Save" button?  Learn about Event Structures!  Don't re-invent basic LabVIEW tools.  Resist writing code for "Add", for example.

 

 

 


Apparently, the Event Structure was exactly what I needed in this case. But in my defense, I was just trying to do what made logical sense to me (dysfunctional? Of course it was, but I tried! ksadkas).

 


Do you understand what the For loop does?  The first time, it builds a bunch of 1D Arrays of size 1, holding the values of the Front Panel controls.  The second time, it overwrites the 1D Arrays of size 1 with (similar) Arrays of size 2.  Note the original data are lost.  The third time, the Arrays of size 2 are overwritten by Arrays of size 3.

 

 


I thought I knew what I was doing, but now I realize I didn’t, and yes, that was exactly my problem.

 


 

I bet that is not what you want.  I'm guessing you want the first Array to have the first value(s).  The next time through the (For) Loop, the output should be the previous Array with the new inputs appended at the end.  What do you know about a Structure (most commonly used with While Loops) that have an Input that was formerly the Output of the previous run?  A related question -- how do you restrict a While Loop to only running once?

 

 


Well, thank God I didn’t bet against you because you’re right! You pointed out some really good questions, so I’ll pass these on to someone really good because I still don’t know kskskas.

Okay, now I’ll give it a try. If I had to guess, I’d say it has something to do with using a shift register to save the previous value, and then I’d make the loop run only once by using a false condition. But again, that’s just a random guess.

0 Kudos
Message 7 of 17
(644 Views)

@Bob_Schor  escreveu:

Since your quantities L, E, DI, DF, CI, CF, etc. appear to be independent measures (even though all of the same type), rather than make an array of these quantities, I'd make a Cluster whose elements are named L, E, DI, DF, CI, CF, etc.  (Hint -- always create a TypeDef from your Cluster and use it everywhere you reference the cluster.  This will save you from writing code that connects CI to DF and DF to CI).

 

Bob Schor


Well, I really didn’t understand this one. How could I create a TypeDef for my cluster?(sorry for this)

0 Kudos
Message 8 of 17
(632 Views)

@MarcusP. wrote:


Well, I really didn’t understand this one. How could I create a TypeDef for my cluster?(sorry for this)


Find your cluster on the front panel or on the block diagram, right-click and choose "Make Type Def.". Right-Click again and choose "Open Type Def." Give the type definition a nice icon and save it as a .ctl file (File->Save).

0 Kudos
Message 9 of 17
(620 Views)

Right-click the cluster..."make type def". After that, you can right-click and "open typedef". This  will open the control editor where you can then save it.

0 Kudos
Message 10 of 17
(617 Views)