Imagine you have a big script running, which takes a very long while to execute.
Then, you need the machine to go down.
You hit CTRL+C, which interrupts the script, and you note the line on which your script was interrupted.
You type "save", which saves everything
the machine reboots, you launch matlab, and "load" back your stuff
the question is : is there a way to start the script from the line it was interrupted ?? (which may be INSIDE a for loop!) Since you have all the variables up and at correct values, it should be possible, but I don't see how to do it...
can someone help me ?
No, MATLAB does not have any method of "checkpointing" program state and restarting it later. You would need to write in the functionality yourself.
Sometimes the easiest way to achieve checkpointing is to write in "transaction processing" style with a State Machine. Instead of having series of loops and routines that call routines that call routines, etc., you flatten out the structure. You have a variable that contains information about the current major and minor "state" (what is being done now), and the routine branches on the major state to blocks of code that know about minor states, and so on, eventually getting to a code location where the current state is fully decoded, at which point the code calls a routine that does just enough work to change state, with the new state being returned upward. And then you go through the next iteration, the state is decoded all over again, and so on. When enough work has been done to be "worthwhile", perhaps when the code figures out that it is at the end of a substate, the code writes out all the information about the current state and current variables.
With this structure, continuing becomes easy: just restore the last saved state with variables, and feed that into the state machine. The state machine won't even notice that there was a downtime in-between (but you might have to re-open files you were working with.)
There are some kinds of problems where a State Machine works out very naturally, compact and efficient and flexible. It is, however, not a programming structure that most people are comfortable with.
At various points (places that take a long time to execute) you could add tests that determine if you need to execute that portion of the code. For example, you could set flags or test for created variables. For loops, you could test the loop variable to see if it exists and if it does to initialize it with the previous value. If you have more than one loop, make sure you use unique variables to identify each loop correctly. You should clear all variables before loading the old data file.