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: 

Adding Rows to Arrays recursivly

Solved!
Go to solution

Hello,

 

Im new to labview and currently stuck on one task. I hope you can help me.

I try to build a VI where one has just two numerical inputs, one button and potentially a display for the resulting array on the front panel.

The first time one presses the button an array with one row and two columns with the two values respectively is created.

Everytime you press the button another row with other values you is added - as often as you want.

 

My idea was an event structure where one can add a row to an already existing Array. I got it working. But Im not getting the part where I have to make it recursive and run it without and pre-existing array.

 

Im very grateful for every help. Thanks

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

You have the right idea with an event structure. You want to combine an event structure, a While loop, and preferrably a case structure to create a State Machine. Your array can be stored in a Shift Register from iteration to iteration and appended to within a specific case or just within the event structure.

 

The Simple State Machine template that ships with LabVIEW is really the best way for new developers to get familiar with LabVIEW while utilizing a semi-scalable architecture.

Here's a broad example of how a state machine works:

  • States: Init, Idle, Exit, DoThing1, DoThing2, DoThing3
  • Each state contains code that might take some time. Ideally, not too long because that's how long your code could be unresponsive.
  • The Idle state contains an event structure for all user interaction.
  • The front panel has a button that says "Do Thing 1".
  1. Loop Iteration 0: Application begins, first state is Init. The Init state does some initialization stuff and tells the application to go to the Idle state when finished.
  2. Loop Iteration 1: Application goes to Idle state. It sits there, waiting at the event structure.
  3. Time goes by, then user presses button "Do Thing 1". There is no code, or minimal code, within this event case. The output of this event state tells the application to go to the DoThing1 state.
  4. Loop Iteration 3: Application goes to DoThing1 state. There is code here that does some stuff. The output of DoThing1 state tells the application to go back to the Idle state.
  5. Loop Iteration 4: Application goes to Idle state where it waits at the event structure again.

Each of the states can tell the application to go to any state they want. Want to reinitialize? Go to the Init state again. Want to end the program? Go to the Exit state. Want each state to trigger another (like a sequence)? Have DoThing1 output DoThing2, which outputs DoThing3,  which outputs Idle.

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 2 of 10
(3,300 Views)
Solution
Accepted by topic author Godeko

@James.M wrote:

You have the right idea with an event structure. You want to combine an event structure, a While loop, and preferrably a case structure to create a State Machine. Your array can be stored in a Shift Register from iteration to iteration and appended to within a specific case or just within the event structure.

 


Here's an old example that you can modify for your purpose. (originally posted here)

 

(you could also use a feedback node instead of a shift register)

0 Kudos
Message 3 of 10
(3,293 Views)

@Godeko wrote:
But Im not getting the part where I have to make it recursive and run it without and pre-existing array.

Make what recursive?  Adding items into an array one at a time does not need recursion.  Is this a homework assignment?  Can you state the problem more clearly or show the homework assignment, if it is indeed homework? 

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 4 of 10
(3,287 Views)

In your subVI, you would use an uninitialized shift register (or better a globally initialized feedback node) and assignment the scalar input and array output to the connector pane

0 Kudos
Message 5 of 10
(3,263 Views)

Sounds like you need to learn about Shift Registers.  Why don't you attach some LabVIEW code (so that we don't do all of your Homework for you).  It seems to me that you are describing an "iterative", not a "recursive", problem (though "tail recursion" and "iteration" do look very similar).

 

Bob Schor

0 Kudos
Message 6 of 10
(3,232 Views)

Thank you very much guys. It will take me some time getting used to the shift registers, but this tool opens many more possibilities.

I got stuck on the idea of recursive functions that I didn't see this option.

 


@altenbach wrote:

@James.M wrote:

You have the right idea with an event structure. You want to combine an event structure, a While loop, and preferrably a case structure to create a State Machine. Your array can be stored in a Shift Register from iteration to iteration and appended to within a specific case or just within the event structure.

 


Here's an old example that you can modify for your purpose. (originally posted here)

 

(you could also use a feedback node instead of a shift register)


Ive modified the example and got it working after a while.

 

Thanks!

0 Kudos
Message 7 of 10
(3,211 Views)

Seems to work. Good!

 

Some points:

  • Please do not maximize the front panel and diagram window to the screen. That's extremely annoying because you can only look at one thing at any given time. You probably need room to also see the help window occasionally. Nobody wants to stare at a gigantic diagram that is 90% whitespace.
  • A good rule is to place the terminal of latch action booleans inside their respective event. This way you can verify that the event actually fired. Especially important with more complicated code. (If the terminal is not in the dataflow and read, it would not reset to false)
  • You might want to initialize your shift register with an empty 2D array constant. Currently, the shift register clears when the stop event executes, but if you would abort the VI, the shift register contents content are retained until the VI leaves memory.
  • Typically you would rename the label and boolean text to something reasonable. "Start Button" and "Start" are not descriptive of the current functionality.
  • Since the array can grow without bounds, I would show the vertical scrollbar of it.
Message 8 of 10
(3,203 Views)
@altenbach wrote:

Seems to work. Good!

 

Some points:

  • Please do not maximize the front panel and diagram window to the screen. That's extremely annoying because you can only look at one thing at any given time. You probably need room to also see the help window occasionally. Nobody wants to stare at a gigantic diagram that is 90% whitespace.
  • A good rule is to place the terminal of latch action booleans inside their respective event. This way you can verify that the event actually fired. Especially important with more complicated code. (If the terminal is not in the dataflow and read, it would not reset to false)
  • You might want to initialize your shift register with an empty 2D array constant. Currently, the shift register clears when the stop event executes, but if you would abort the VI, the shift register contents content are retained until the VI leaves memory.
  • Typically you would rename the label and boolean text to something reasonable. "Start Button" and "Start" are not descriptive of the current functionality.
  • Since the array can grow without bounds, I would show the vertical scrollbar of it.

Thank you very much. At the moment its quit messy as I just try to get it working in any way, but I will keep it in mind for the final product as its going to be used my external users.

 

I have one more question or problem which came up working with the new array mechanic.

I need to give a small explanation on my project to understand it.

My project is programing and setting up a galvo system, where one can basically set up a angle of the galvos = output voltage of the DAQ.

As I already stated, this system has to be useable by other scientists which dont have any clue about the program.

What I want is that one can set different voltage values (first column) and times (second column). The output switches the voltages for the respective times. When it reached the last value it starts over again.

I got it working with a simple array one can modify (see voltageswap NI forum.vi), but I want it to feel more intuitiv. Thats why I wanted this function I asked my initial question for.

I tried to implement the solution I linked in my last post, but the problem is now that:

  • After reaching the last voltage, the for loop doesnt restart
  • It only adds new rows when the loop reaches its end. I want it to restart the loop as I set a new value

You can see it in the file voltageswap add NI forum.vi

The second while loop with the voltage analog input is just there to link the output to the input and check if everything is alright (which aperently isnt as already stated).

 

Im using a USB-6002 DAQ but that shouldnt be the problem.

I probably didnt understand the event and while-loop mechanics right and would be really happy if one can give a clue on what is wrong.

 

Thank you!

Download All
0 Kudos
Message 9 of 10
(3,123 Views)

Hi Godeko,

 

I tried to implement the solution I linked in my last post, but the problem is now that:

  • After reaching the last voltage, the for loop doesnt restart
  • It only adds new rows when the loop reaches its end. I want it to restart the loop as I set a new value

THINK DATAFLOW!

The FOR loop will restart as soon as the While loop starts the next iteration.

The While loop will start the next iteration as soon as all its included functions are finished.

The event loop is finished as soon as you press the Start button - as you configured it…

 

On your VI:

Why don't you use AutoCleanup?

Why do you index those array values in a very complicated way?

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 10 of 10
(3,119 Views)