How to exit a nested if statement

Ah, on my old motorcycle forum we used OP as 'overhead poster', the poster above you. And I can get along with matlab fine, that's just commands for maths and I understand that. My problem is trying to change code that I didnt write, with libraries that have other commands etc. I've resorted to basics and writing out the if statements on paper, and I'm back to where I started after messing up the 'I've got it working' code. Now I'm trying to look up how I can get this code to;

Run the program as usual, giving the accelerations.
Pause the program when pressing a button.
Save the results.
Restart the data collection
Repeat as necessary

Looking at doing this through commands in the serial monitor or through external buttons. I think the serial monitor would be more useful

Thanks for your help