This question already has an answer here:
How to abort a running program in MATLAB?
(1 answer)
Closed 7 years ago.
I write a long running script in Matlab, e.g.
tic;
d = rand(5000);
[a,b,c] = svd(d);
toc;
It seems running forever. Becasue I press F5 in the editor window. So I cannot press C-Break to stop in the Matlab console.
I just want to know how to stop the script. I am current use Task Manager to kill Matlab, which is really silly.
Thanks.
Matlab help says this-
For M-files that run a long time, or that call built-ins or MEX-files that run a long time, Ctrl+C does not always effectively stop execution. Typically, this happens on Microsoft Windows platforms rather than UNIX[1] platforms. If you experience this problem, you can help MATLAB break execution by including a drawnow, pause, or getframe function in your M-file, for example, within a large loop. Note that Ctrl+C might be less responsive if you started MATLAB with the -nodesktop option.
So I don't think any option exist. This happens with many matlab functions that are complex. Either we have to wait or don't use them!.
If ctrl+c doesn't respond right away because your script is too long/complex, hold it.
The break command doesn't run when matlab is executing some of its deeper scripts, and either it won't log a ctrl sequence in the buffer, or it clears the buffer just before or just after it completes those pieces of code. In either case, when matlab returns to execute more of your script, it will recognize that you are holding ctrl+c and terminate.
For longer running programs, I usually try to find a good place to provide a status update and I always accompany that with some measure of time using tic and toc. Depending on what I am doing, I might use run time, segment time, some kind of average, etc...
For really long running programs, I found this to be exceptionally useful
http://www.mathworks.com/matlabcentral/fileexchange/16649-send-text-message-to-cell-phone/content/send_text_message.m
but it looks like they have some newer functions for this too.
MATLAB doesn't respond to Ctrl-C while executing a mex implemented function such as svd. Also when MATLAB is allocating big chunk of memory it doesn't respond. A good practice is to always run your functions for small amount of data, and when all test passes run it for actual scale. When time is an issue, you would want to analyze how much time each segment of code runs as well as their rough time complexity.
Consider having multiple matlab sessions. Keep the main session window (the pretty one with all the colours, file manager, command history, workspace, editor etc.) for running stuff that you know will terminate.
Stuff that you are experimenting with, say you are messing with ode suite and you get lots of warnings: matrix singular, because you altered some parameter and didn't predict what would happen, run in a separate session:
dos('matlab -automation -r &')
You can kill that without having to restart the whole of Matlab.
One solution I adopted--for use with java code, but the concept is the same with mexFunctions, just messier--is to return a FutureValue and then loop while FutureValue.finished() or whatever returns true. The actual code executes in another thread/process. Wrapping a try,catch around that and a FutureValue.cancel() in the catch block works for me.
In the case of mex functions, you will need to return somesort of pointer (as an int) that points to a struct/object that has all the data you need (native thread handler, bool for complete etc). In the case of a built in mexFunction, your mexFunction will most likely need to call that mexFunction in the separate thread. Mex functions are just DLLs/shared objects after all.
PseudoCode
FV = mexLongProcessInAnotherThread();
try
while ~mexIsDone(FV);
java.lang.Thread.sleep(100); %pause has a memory leak
drawnow; %allow stdout/err from mex to display in command window
end
catch
mexCancel(FV);
end
Since you mentioned Task Manager, I'll guess you're using Windows. Assuming you're running your script within the editor, if you aren't opposed to quitting the editor at the same time as quitting the running program, the keyboard shortcut to end a process is:
Alt + F4
(By which I mean press the 'Alt' and 'F4' keys on your keyboard simultaneously.)
Alternatively, as mentioned in other answers,
Ctrl + C
should also work, but will not quit the editor.
if you are running your matlab on linux, you can terminate the matlab by command in linux consule.
first you should find the PID number of matlab by this code:
top
then you can use this code to kill matlab:
kill
example:
kill 58056
To add on:
you can insert a time check within a loop with intensive or possible deadlock, ie.
:
section_toc_conditionalBreakOff;
:
where within this section
if (toc > timeRequiredToBreakOff) % time conditional break off
return;
% other options may be:
% 1. display intermediate values with pause;
% 2. exit; % in some cases, extreme : kill/ quit matlab
end
Related
I have a main function in MATLAB which consists of a while(1) loop. Also, I have subscribed to a ros image_raw topic which updates a callback function in MATLAB.
Whenever I press CTRL+C, I should hold it for a long time to work. I have tried onCleanUp but it is not working, either.
How can I quickly stop the program when running?
Setup: Windows10, MATLAB 2017b
MATLAB responds to CTRL-C only at certain times. Lines of code in the MATLAB language respond immediately, but If you are calling into a library not written in MATLAB, then the CTRL-C will only take effect when control returns to MATLAB. (This applies even to some functionality shipping with MATLAB such as linear algebra routines which call our to external libraries like MKL).
Basically, there's probably not much you can do if the function you're calling doesn't respond immediately. You shouldn't need to hold down CTLR-C though, the request should be queued.
I don't think onCleanup is relevant here - that's for dealing with the effect of the CTRL-C.
Try Ctrl+Break (it may be Ctrl + Fn + Pause based on your keyboard). If that doesn't work, it may just be a computational limitation of MATLAB; my other suggestion would be running ROS in the Windows SubSystem for Linux (WSL) and running your subscriber in MATLAB, or move the ROS master and subscriber to the WSL and use python or another language for the subscriber.
I'm using MATLAB R2014b. I designed a GUI that have a loop using parallel computing. First I will set the number of loops in this GUI and my program will start. Suppose that I want stop the program in middle of process. I should press CONTROL+C to stop MATLAB. I put a button to stop it and call a function in loop but it doesn't work when my program is running. These isn't any respond to this button when my program is running.
How can I solve this problem?
Thanks.
I presume by "parallel computing" you mean that your code is running is running in a parfor loop. The parfor loop is a synchronous construct which does not allow any other MATLAB commands to execute. If you wish to allow interactive use via a GUI, you need to use parfeval instead. This example shows how you can terminate parallel processing early.
One thing to bear in mind when using parfeval is that each call you make to that is a remote invocation, so you need to divide your problem into "sensible" sized chunks.
I run a pretty computationally expensive genetic algorithm with MATLAB. The code has been running for 3 whole days, and I am pretty sure it gets stuck somewhere, because it is not printing out the progress information for debugging purpose.
I now wish to stop it. I did CTRL+C, but no luck. The bottom left of the window still displays "Busy".
I cannot simply quit the whole MATLAB, because I need to find out where it gets stuck by inspecting the variables in the variable window.
Given the CTRL+C is not working, how can I
stop the execution, OR
save the variables for inspection purpose?
Sometimes ctrl-C stops working if you have a memory over-allocation problem -- if you are trying to allocate a matrix that doesn't fit in memory, and so virtual memory begins thrashing.
It's also likely that crtl-C won't work while execution is passed to COMSOL.
I think you have little choice now but to kill matlab and try to debug by either stepping through the code or inserting fprintf statements.
There is something very simple that can be very upsetting occasionally.
I sometimes want to keep track of the outputs of an algorithm at each iteration, so within a for loop, I use disp command of MATLAB to output some information. However, although there is quite a bit of time between the calling of dispcommands, the MATLAB command window falls behind and I need to use the mouse to keep up with it manually. Do you know if there is any way to have it not fall behind?
Thanks,
It really shouldn't fall behind, but I could see this happening if the computations are intensive and the MATLAB JVM, which drives the GUI, does get the resources to update the command window.
The following command may be of help:
drawnow update % or just drawnow
According to the documenatation page, this "causes only user-interface objects to refresh, if needed". If that fails, try just drawnow with no arguments to see if it helps to flush the entire system event queue, including graphics updates.
If that doesn't work you could insert a pause(0.01) or something similar as a last resort.
It shouldn't fall behind if you don't use the mouse at all. However, I often use the waitbar for this purpose.
FEX also has several text & GUI progress bars.
Is there a way to end a matlab process that is taking too long to run?
ctrl+alt+delete is all I know right now and that shuts downt he program entirely.
It's Ctrl+C.
Apparently it's inconsistent at times:
http://www.mathworks.com/support/solutions/en/data/1-188VX/
Control C is the answer. It will break in. However, there are cases where it still may take a while to do the interrupt. For example, if the process is trying to solve a huge linear system of equations or allocate a huge block of virtual memory, then matlab will not see the interrupt until the solver returns control to matlab itself. So it may take a while before the break happens. If this is just a long running iterative process, then the break will happen quickly.