MATLAB Answers

0

Restart a for loop with a different starting point in Matlab app designer

Hi,
I have attached an app (vapp1.mlapp) which displays 'Display 1, 2, 3...10' for each iteration when Begin button is pushed.
I wanted to repeat a particular iteration and then complete the iterations till the end (10). For example, after Begin button is pushed, suppose currently the display is 'Display 6', I want to restart the display with 'Display 5' and go upto 'Display 10' when I push Restart from Last Step Button (It uses return function).
The repeat function works fine, but after displaying upto the end of the iteration ('Display 10'), it completes the iterations which were stopped when Restart from Last Step Button was pushed.
In the example, what I want is to display 'Display 1' to 'Display 6' and then after pressing Restart from Last Step, dispaly 'Display 5' to 'Display 10' and then stop. But after displaying 'Display 10' it starts displaying from 'Display 7' to 'Display 10'.
Kindly suggest a solution.

  0 Comments

Sign in to comment.

1 Answer

Answer by Bhargavi Maganuru on 19 Aug 2019
 Accepted Answer

You can use flag variable to stop the execution of BeginButtonPushed function in your code after pressing Restart from Last Step button.
Add following lines of code:
Add global variable named flag in all the functions: global flag;
In line 35,44 add: flag=0;
In line 53 add: flag=1;
In line 18, modify the if condition to: if (repeat==1 || flag==1)
Hope this helps.

  4 Comments

Show 1 older comment
Could you please tell why the for loop restarted from where it stopped (when Restart from Last Step button was pushed) in the original code eventhough
return
exited the loop? It was as if return just paused the remaining steps to restart later from where it had left.
Thanks.
Hi,
When you press Begin button,the function BeginButtonPushed will starts execution and now when you press Restart from Last Step button(say at 'Display 6'), the function RepeatLastStepButtonPushed will complete execution( displays 'Display 5' to 'Display 10'), now Displayy function called in BeginButtonPushed will still continue to execute from where it got paused(displays 'Display 7' to 'Display 10').
In your answer,
''....now Displayy function called in BeginButtonPushed will still continue to execute from where it got paused(displays 'Display 7' to 'Display 10').'',
isn't the return function supposed to exit the for loop rather than just pause it?
Regards

Sign in to comment.