LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Controlling Index to Replace Array Inside FOR Loop

Solved!
Go to solution

Hi All,

 

I'm relatively new to LV and am struggling with the logic behind a concept.

 

The program attached aims to:

1) Generates a 1x5 1D array when the "OK" button is pressed.

2) Adds a new 1x5 1D array into a 2D array beneath the previous array when "OK" is pressed again.

3) Changes an n-index array when "RETEST" is pressed, where n is given by a numeric control.

4) Continues to add a 1x5 1D array beneath the last array after "RETEST" has complete and "OK" is pressed again.

 

The issues with this program:

1) The FOR loop count (e.g. 4) is meant to define the number of numeric arrays there should be in total. When RETESTing, the FOR loop increments, even though it replaces the correct array. So, if I need an 10x5 numerical array I will "OK" 9 times, then need to "RETEST" 1 time, then the FOR loop ends and there is a 9x5 numerical array.

2) After doing a "RETEST", pressing the "OK" is meant to give a 1x5 numerical array where index 0 is n-1 row. However, as I've linked the FOR loop increment with the 0th element and issue 1) occurs, the outcome when I "RETEST" once is:

test0 test1 test2 test3 test4

1       x       x       x       x

2       x       x       x       x

3       x       x       x       x

4       x       x       x       x

6       x       x       x       x

 

Is there a method of achieving the aim with a FOR loop and fixing the issues or will an entirely new method be required?

 

Download All
0 Kudos
Message 1 of 7
(2,422 Views)

1. Because of the retest capability, you should NOT be using a FOR loop: You should be using a WHILE loop.  The retest means you do not know how many times you will be performing this action; therefore use a WHILE loop.  Also with this, you cannot rely on the iteration terminal (the 'i').  Instead, you need to keep the current test iteration in a Shift Register.

 

2. Learn to use an Event Structure.  It will make your life A LOT easier.


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 2 of 7
(2,407 Views)

@SDuffyLV wrote:

I'm relatively new to LV and am struggling with the logic behind a concept.


Time to hit the reset button and learn about dataflow, state machine architecture, and proper coding practices.

 

In addition to what has already been said:

 

  • None of your sequence structures are needed.
  • None of you value properties are needed.
  • None of your inner while loops are needed.
  • there is a +1 primitive
  • There is a -1 primitive
  • Don't use orange for integers. Mind your representations.
  • Keep your arrays flat in memory. Non need to delete, insert, reshape, and such.
  • There is way too much duplicate code. How many instances of the column headers do you need?
  • SubVI: never operate on paths as strings, use "build path". Formatting is easier than long concatenation functions. 
  • SubVI: What's "V" for and why is it not a connector?
Message 3 of 7
(2,374 Views)
Solution
Accepted by topic author SDuffyLV

Here's a quick code skeleton that can hopefully give you some ideas. (Arguably simpler ;))

 

 

altenbach_3-1602528968158.png

 

 

 

Lots of things can be improved, but start with that.

Message 4 of 7
(2,362 Views)

Good advice, thank you.

 

I was debating using Event structures but couldn't get them working on an initial setup and moved to the FOR loop method.

0 Kudos
Message 5 of 7
(2,355 Views)

Yes, this is going to take some time to get used to the state machine architecture. However, I'm learning the LV Core 1 course, so this should certainly help.

 

Wow, that's surprising how wrong I went with this design. I really appreciate the input with this. It's a good learning curve.

 

There's supposed to be 1x column header for the titles "test0, test1...". I see the duplication could be unnecessary but was convenient for my method at the time.

 

V is just a placeholder name that defines the part of title of the csv to be created.

0 Kudos
Message 6 of 7
(2,350 Views)

This is PRECISELY what I was aiming to do. It's surprising how much more simplified and clean this code is compared to my original one.

 

I'm going to extend this to have a hard limit on the quantity of rows for completion of my aims (or just make Test invisible when a certain quantity is reached).

 

Massive kudos for this. It's greatly appreciated!

0 Kudos
Message 7 of 7
(2,341 Views)