Matlab pause without breakpoint

I have a script that runs a lot longer than I expected, it has been running for the past 3 days and has achieved only 55% progress.

I would be happy to stop him by about 67% (I can live without the remaining 33%). But if I stop it now (ctrl + c or ctlr + break), I will lose all the data.

So, is there a way to pause Matlab, possibly in debug mode, so that I can check variables without losing data?

+7
matlab
source share
5 answers

Command (must be entered manually before you begin your work!)

dbstop if error

should catch ctrl-c and leave you in debug mode.

+2
source share

I assume you are doing something iteratively here and not relying on the matlab built-in function.

As I usually solve the problem, you should have an iteration counter and an if on that counter - when the condition is met, the operator has a breakpoint.

Something like that:

 itCounter = 0; itHalt = 100; while (someCondition) if (itCounter == itHalt) itCounter = 0; %<= Put a breakpoint here else itCounter = itCounter+1; end % Here you calculate away whatever you need to calculate end 

So in every itHalt iteration you get a breakpoint. In addition, since we are dealing with Matlab, you can change the value of itHalt as you see fit as soon as the breakpoint is removed.

0
source share

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.

0
source share

Ok, just rephrase what I said in the comments, with materials from other users who commented. If your script is a MATLAB script, (not a function), all variables will be accessible from the workspace until you explicitly name "clear" in the script if the script is stopped. In the usual case, ctrl + c will end the script. The MATLAB variables used in the script will still be available from the MATLAB workspace.

0
source share

I don’t think there is anything that you can do while the code is already running, if you don’t have some hooks in advance. Some of these other suggestions are good for this. Here is another one that I like: let's say you leave for the night, but come back the next day, so you want your code to work for 14 hours and then stop and wait for you with how much data it received at that time .

 start_time = now; final_time = start_time + 10/86400; % datenums are in days in Matlab, so +14/24 for 14 hours % alternative: final_time = datenum('12-Aug-2015 09:00:00'); while now < final_time % do work disp('Working...') pause(1) end % potential clean up code to save results disp('Clean up code.') 
0
source share

All Articles