Disable Matlab focus stealing, without modifying Matlab script - matlab

I hava Matlab script running in the background. It keeps popping up pogress bar windows as the computation progresses. This is very annoying.
Problem is I did not write the Matlab script myself, and it is a very long and complicated piece of code that I don't want to mess with. So how can I prevent Matlab from stealing focus, without modifying the Matlab script? Hopefully some Matlab setting will let me do this, without modifying the script itself.
In case it matters, my PC is running Xubuntu.

Some ideas to avoid figures:
Open a single worker (requires parallel computing) and run your script on it. Workers automatically don't have a GUI
matlabpool 1
warning('off','MATLAB:Completion:AllInputsExcluded') %turn off warning
spmd, yourfunction, end
Use the matlab startup parameters to disable figures
matlab -noFigureWindows
or start matlab as a command line tool, running your function, saving the workspace and exiting.
matlab -nodisplay -nosplash -r "yourfunction;save('result.mat');exit"

Related

MATLAB, overwriting in command window

I am running some code in MATLAB and basically there is a lot of output, that is going into the MATLAB command window.
However, when it gets so many thousand, it starts to overwrite, so the output at the very start disappears from the MATLAB command window?
Any suggestions?
Thanks in advance!

Using command window in Octave debug mode

When my running script reaches a break point, it pauses as expected. When I try to execute a command using the command window like plotting a vector, modifying a value or even displaying it, the script resumes execution -- a behavior not found in Matlab.
Is there a way around this?

MATLAB GUI doesn't respond when my codes are running

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.

Matlab plots with a very low resolution when using -nodesktop option

I'm having trouble with the resolution of matlab plots when the function is run via a cronjob using the -nodesktop option. The function plots, and saves (using the print function) a number of figures. When run from the matlab desktop, they plot and save according to the specified resolutions with no problems. But when run via a cronjob with the -nodesktop option, the resolutions are very poor (low).
This is essentially what I'm currently doing:
fh = figure;
set(fh,'Position',[0 0 1360 470]);
plot(somedata);
print(fh,'-dpng',figpath,'-painters');
Note that I've also tried specifying a print resolution:
print(fh,'-dpng',figpath,'-r300');
But this ends up with corrupt files. I'm not sure why. I'm using -painters because I read somewhere that if the plot is somewhat complicated it can default to opengl which ruins prints but this doesn't seem to be the issue (with or without the option, the plots are the same).
I've also tried:
set(fh,'PaperPositionMode','auto')
which does not solve the problem.
The cronjob runs the following command:
/usr/local/bin/matlab -nodesktop -r "startup; perform_plots; exit"
Any help would be much appreciated!
I figured out the issue.
By running matlab as a cronjob, the DISPLAY environmental variable wasn't set. As a result there was no X display for matlab to utilise. I don't know how it still managed to plot anything at all but it did and did so with a very low resolution.
The solution is to set the appropriate display before hand. e.g.:
#!/bin/bash
export DISPLAY=:1.0
/usr/local/bin/matlab -nodesktop -r "startup; perform_plots; exit"
In case you want to run it on a box that doesn't have an X server, you can set up a dummy X server using Xvfb (X virtual frame buffer). This also has the advantage of using a display separate to the one you may be working on so jobs can run in the background without plots randomly appearing while you work.

How to break matlab script execution? [duplicate]

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