I am thinking of an alternative solution:
Let there be a script, which mainly consists of a main loop.
The script periodically writes information about the execution status (for example, the number of iterations) to the log file.
In addition, the script periodically reads a number from the input file.
1 meaning "continue" 0 meaning "stop the script execution"
At the beginning of the simulation, 1 is written to the file.
The user can read the log and at some point may decide to stop the script.
To do this, it should simply change 1 to 0 in the file, how to save it.
The if section indicates the value read on it.
If 1, nothing appens and the script continues running. If 0, a break statement terminates the main loop and the script stops.
Just before the break statement in the if section, the script saves the entire workspace to a .mat file.
Now the user has access to MatLab (he can close MatLab) and can look, for example, on the output files generated up to this point in the script, process them, make the somo plot, etc.
He can then decide to continue executing the script from the point at which it was stopped.
At the beginning of the script, the variable controls how the script should be executed:
Mode 0: start from the beginning Mode 1: resume the script
In the if - else section, user selection is highlighted.
In particular, if mode 1 is selected, the script loads the previously saved workspace (stored in the .mat file), then the value of some script variables is set to the old values.
As an example: a script was stopped when the for loop index was, say, 100.
if the for loop is defined as
for i=start_loop_1:100000
in mode 1 if, start_loop_1 is set to i + 1 (the value of i was saved in the .mat file).
This allows the loop to “continue” execution from where it was stopped.
To effectively “restart” the script, some other variables used in the script may be required to be controlled in the same way in the “Mode 1.” section.
In the case of a "large", "complex" script, this can be difficult, but ... not impossible
This solution is implemented in the following script.
I see a potential criticality in the unsuccessful case where the user saves a file containing 1.0 while the script reads it.
% Flag to select the running mode % Mode 0: start from the beginning % Mode 1: resume the running continue_my_script=1; % if "Mode 1" has been selected, the "old" workspace is loaded and some % variables are properly set if(continue_my_script == 1) load my_script_data start_loop_1=i+1; start_loop_2=1; % if Mode 0 has been selected some variables are set to their default value else start_loop_1=1; start_loop_2=1; % counter to enable writing of the log file cnt_log=0; % counter to enable reading the "go / no go" input file cnt_go=0; end % Definition of the condition for writing the log file (in this case, a % certain number of iterations") log_iter=13; % Definition of the condition for reading the "go / no go" input file (in % this case, a certain number of iterations") go_nogo_iter=20; % Starting point of the "real script" for i=start_loop_1:100000 % Increment the log counter cnt_log=cnt_log+1; % if "log_iter" have been done, update the log file if(cnt_log == log_iter) cnt_log=0; t=clock; fp=fopen('my_script_log.log','wt'); fprintf(fp,'i= %d at %d %d %f\n',i,floor(t(4)),floor(t(5)),t(6)); fclose(fp); end % Another loop of the script for j=start_loop_2:100000 a(i,j)=sqrt(i); end % Increment the "read input file" counter cnt_go=cnt_go+1; % if "go_nogo_iter" have been done, read the go_nogo input file if(cnt_go == go_nogo_iter) cnt_go=0; fp1=fopen('my_script_go.log','rt'); go_nogo=fscanf(fp1,'%d'); fclose(fp1); % If the user made the decision to stop the execution, save the workspace % and exit; otherwise ... do noting, just continue running if(go_nogo == 0) save my_script_data break; end end end
Hope this helps.