LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with the "INSERT" button.

Solved!
Go to solution

Hi,

I have a problem in my homework. I am required to do the following:

'original numbers' array contains zeros. Let LabVIEW fill an array "original numbers" by random integers from 10 to 90 at a rate 1 number/0.5 seconds by replacing each zero at a time from top to bottom. (DONE)

user fills in 10numbers in an array 'numbers to be probably inserted'. Then the user decides if he needs to change a generated number that he doesn't like by changing the 'Insert' array.

Then he presses the INSERT button and a resulting array would appear containing the elements the user changed......

This video might clarify:

http://www.youtube.com/watch?v=dDXo2ABqNM4

 

The problem I am facing is the INSERT button. In the video, the moment it is switched the program will continue to the last part.

I am attaching the vi. I think there are no logical mistakes in the program except the INSERT case structure.

Thank You.
P.S. The version is LabView2010 ; I'm getting error trying to save to previous versions sorry.

0 Kudos
Message 1 of 10
(3,008 Views)

Your VI reads the Insert button immediately at which time it is false.  You need to have your "last part" in an event structure that waits to execute until the Insert button is pressed.

0 Kudos
Message 2 of 10
(3,004 Views)

I have replaced the case structure with an event structure, but its the first time I use an event structure. 
I have seen the help examples but where should I wire the event? Type Time?

0 Kudos
Message 3 of 10
(2,998 Views)

The event will be the Insert Button Value Change event.

0 Kudos
Message 4 of 10
(2,992 Views)

@A.A.A. wrote:

I am attaching the vi. I think there are no logical mistakes in the program except the INSERT case structure.


At this stage of learning, I would stay away from the event structure. Let's keep it simple for the moment.

 

There are several mistakes that have nothing to do with the "insert" button:

 

  • Your "round to nearest" is incorrect and will cause a non-equal distribution. The numbers are not truly random because the lowest and highest number will occur at only half the probability of all other numbers. (Instead, you need to go one higher and round to -inf).
  • Your loop and data structures are incredibly convoluted for this simple task.
  • Your "changed number" indicator is possibly lying to you. (for example if you replace a number with the same value, you get a TRUE, even though the new value is the same).
  • "Insert" is the wrong word for the operation, you are replacing values (In LabVIEW, inserting an element into an array would keep all existing element, growing the array by one. Also tell this to your teacher! 🐵
  • ...

 

Here is a quick draft showing some alternative methods. There are a few parts that you might not understand inmmediately, but you probably have all weekend to study it. 😉 If you do and understand the reason for every single function, you have learned important new programming skills! 🙂

 

Feel free to ask questions.

 

(Study it well and then create your own version. Remember, your teacher reads this forum too!)

0 Kudos
Message 5 of 10
(2,989 Views)

@altenbach wrote:

  • Your "round to nearest" is incorrect and will cause a non-equal distribution. The numbers are not truly random because the lowest and highest number will occur at only half the probability of all other numbers. (Instead, you need to go one higher and round to -inf)


I understood your point about non-equal probabilities for the integers at the interval.


@altenbach wrote:

  • Your loop and data structures are incredibly convoluted for this simple task.


I have done so to initialize the arrays full of zeros yet the way you did it way easier.  How did u initialize them to zeros?(from where did you get the palettes? I have attached a picture clarifing my question.

 

I have understood most of the program especially the while loop + Replace! and I made my own version of this code but I have still some questions regarding the VI.

 

  • What does it mean to disable auto-indexing?
  • I didn't understand why we used the reshape array palette. What are the dimensions of the input and the output? Very nice way how you made a length 10 zero array. (i'll use that when i figure the solution of the dimension problem.)
  • Did you use the 100ms wait in order not to affect computer performance or for other reason?
  • How where you able to connect the 'changed numbers' array to Not Equal Palette?
  • From where did you get the "replace" button? I think my "insert" button does the oppositeof what it should do.
Download All
0 Kudos
Message 6 of 10
(2,972 Views)
Solution
Accepted by A.A.A.

@A.A.A. wrote:
  1. What does it mean to disable auto-indexing?
  2. I didn't understand why we used the reshape array palette. What are the dimensions of the input and the output? Very nice way how you made a length 10 zero array. (i'll use that when i figure the solution of the dimension problem.)
  3. Did you use the 100ms wait in order not to affect computer performance or for other reason?
  4. How where you able to connect the 'changed numbers' array to Not Equal Palette?
  5. From where did you get the "replace" button? I think my "insert" button does the oppositeof what it should do.

Your code sitll has major flaws. Your questions are very basic and I recommend to do a few tutorials.

 

  1. If you wire an array across a loop boundary, the tunnel can have two modes (plain (solid square) or autoindexing (contains a small set of square brackets)). In your case, you are autoidenxing the 1D array, creating a 2D array (double-line) at the output of the first loop. You are generating 10x too much data! (create an 10x10 indicator on the 2D array to see what you are actually doing!). You can right-click a tunnel to switch between indexing modes. A plain tunnel transports the array data unchanged. An autoindexing output tunnel, creates an array with a higher dimension, in your case a 2D array from a 1D array with one new row per iteration. If you autoiindex a scalar, you get a 1D array with one element per iteration. An autoidexing input tunnel does the reverse. If you wire a 1D array to a FOR loop using autoindexing, you get a scalar per iteration and the FOR loop will stop once the array runs out of elements. Is it equavalent (with some important differences) to wiring without autoidenxing, then using "index array" with the index wired to [i] as you currently do. In your case, you chould get the 1D array from the shift register of the first loop, it has all the information(!), and disable autoidexing at the boundary of the second loop and the outcome would be the same, except you use only 10% of the memory.
  2. Once you are autoidexing at a loop boundary, a FOR loop iteration count will be determined by the size of the smallest autoindexing array. This would be a problem if you later want to change the code to operate on e.g. 20 elements, because the other two arrays in my code would force the loop to stop after 10 iterations. "Resize array" can be used for padding/trimming arrays and in this case we are forcing them to be the lenght on the main array to ensure that the FOR loop does not stop prematurely. In your case, the replace and switch arrays are empty by default, so the loop would not spin at all unless you would enter some values. "resize array" brings them up to size. 😉
  3. Yes, you need a small wait either inside the other case or elsewhere in the while loop. Without a wait, the basically empty loop will spin millions of times per second, consuming all CPU in the process while doing nothing. No human can click a button with that kind of time resolution, so a 100ms wait is not noticeable, but will drop the CPU use to negligible levels. Your computer has dozens of processes running at any given time. Imagine how well it would work if all programs would consume all CPU they can possibly get? 😄
  4. I am comparing two arrays, and the output will be a boolean array. You are only comparing two scalars and you won't get an array. You also don't need the "AND", because if the values are different it has to be true anyway. In your case you would need to carry a boolean array similar to the number array, replacing elements as you go.
  5. A button is a button is a button. There should be no difference. You can change the label, boolean text, default value, and mechanical action to match your requirements.

@A.A.A. wrote:
I have done so to initialize the arrays full of zeros yet the way you did it way easier.  How did u initialize them to zeros?(from where did you get the palettes? I have attached a picture clarifing my question.

These are local variables. You don't need to find them in the palettes. Simply right-click a terminal and "create local variable". Voila!

(Local variables can be used to read from an indicator or write to a control. However, because they break dataflow, you can generate dangerous race conditions. They also force extra data copies in memory. Don't use them like "variables" in text based code. Except for initialization and user interface interactions, they should be used sparingly and are rarely needed. In principle, my code has a theoretical race condition because the order of operations is not determined between the three independent code sections. For example if the initializion would happen after the second loop executes, you would get an unexpected result. It will never happen in this particular case, but in other scenarios it could be a problem.)

Message 7 of 10
(2,961 Views)

I understood your point on reshaping thearray.

I'm almost done with the VI but I have still a problem with the changed numbers array.(if the element of the original number is equal to the element of the 'numbers to be probably inserted' array)

If I want to replace two equal elements it is still giving me true.


@altenbach wrote:
4.        I am comparing two arrays, and the output will be a boolean array. You are only comparing two scalars and you won't get an array. You also don't need the "AND", because if the values are different it has to be true anyway. In your case you would need to carry a boolean array similar to the number array, replacing elements as you go.



0 Kudos
Message 8 of 10
(2,953 Views)

You are just reading the value of the "Insert?" array.

 

Instead compare the orange wire going into the bottom of the select primitive and compare it with the output of the select primitive if they are not equal, the value has changed.

0 Kudos
Message 9 of 10
(2,946 Views)

I made a mistake in the position of replace array subset.Smiley Surprised

 

I fixed it and the VI is working.

Thank You altenbach! Smiley Happy

 

 

0 Kudos
Message 10 of 10
(2,940 Views)