How to save results
    5 views (last 30 days)
  
       Show older comments
    
    Meriem Ben Kalia
 on 19 Aug 2020
  
    
    
    
    
    Commented: Meriem Ben Kalia
 on 22 Aug 2020
            Hello everyone,
I have a very long Code that need much time to simulate, I want to the save all variables (it didn't finish the simulation)  and I want to resimulate with the same variables saved to obtain the result of the code .
N.B: I create 3 functions and main in my code 
0 Comments
Accepted Answer
  Walter Roberson
      
      
 on 20 Aug 2020
        
      Edited: Walter Roberson
      
      
 on 20 Aug 2020
  
      One technique to do this is to convert your for loops into while loops, like this:
if exist('recover.mat')
    load('recover.mat');
else
    I = 1;
    J = 1;
    K = 1;
end
while I <= max_I
    while J <= max_J
        while K =< max_K
            do something
            K = K + 1;
            save recovery.mat
        end
        K = 1;
        J = J + 1;
    end
    J = 1;
    I = I + 1;
end
%when you get here you are done, so no need to set I=1
That is, instead of initializing the loop variables to 1 immediately like you would with
for I = 1 : max_I
    for J = 1 : max_J
    end
end
you continue on from whatever current values of I, J, K are active, and you do not reset those values until the end of the corresponding loop when you are setting up conditions for the next iteration of the enclosing loop.
If there is a failure, then the code should effectively end up restarting the computation from the beginning of the innermost loop iteration whose results were not already saved.
If some of the loops are short and fast you might not want to bother saving in the innermost, as saving takes time. You might, for example, want to move the save to after the J = J + 1, if the K loop is fast enough.
More Answers (0)
See Also
Categories
				Find more on Loops and Conditional Statements in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
