This is a general Matlab how-to, mostly directed towards Linux use. For example code and code specific to our lab please go to Matlab. For instructions on logging into the cluster and running processes in the background, see Backgrounded Processes at the bottom of this page. If you want to run processes, locally or on the cluster, that make use of the parallel toolbox, please see Using the Parallel Toolbox. For a nice tutorial and example code using Matlab and the Psychtoolbox, see Michael Silver's web site, which is based on Ione Fine's tutorial (her web site) (download her tutorial). To install Matlab on a Mac, you need X11 installed, more info at HowTos.MacX.
From our linux machines, one can access Matlab in three ways, from the command line, Xemacs or as a gui. The gui is pretty much identical to the pc Windows version.
To start the matlab gui type
matlab11 & in the xterm window. This will start the latest matlab; to see which matlabs are available look in the directory /usr/local/bin. Unfortunately, the gui has the annoying habit of appearing too high on the screen. To lower the gui, press the alt key and use the mouse (I found I also have to also press the mouse button) to move the window.
Very simple, open a terminal, and type
For those who can't get use to the graphical stuff, someone wrote some impressive code for (X)emacs to use it as a editor/debugger. This matlab-mode does a lot of fancy things including:
To get the most out of using matlab through xemacs, you should have a .emacs file in your home directory with matlab settings. This will ensure colors to be set for easy programming and other useful features. If you load an m-file into xemacs, and it is not color-coded for matlab, or you can't start matlab or matlab-shell, see your system administrator for a .emacs file.
To enter matlab mode use
M-x start-matlab (M is meta - usually the esc key and/or the alt key, sometimes the windows key). To get a long list of options available, type
M-x matlab- and try a self-completion (tab). You might want to spend some time trying various features.
One of the nicer features is being able to move a line or a block from one buffer window to another, this way you can run a small portion of code at a time from a file in matlab. To move a line at a time, move the cursor to the line you want to move and use
C-c l (ctrl and c simultaneously and then l, that's an el, alone). To move a block, highlight the block (use the mouse or C-spc), and use
A couple more useful matlab-xemacs tricks are auto-indenting and auto-commenting. If you hit the tab button, the line will be automatically indented the correct amount, according to the for/while loops, if statements, etc.
C-M-\ (that is Meta or alt, not capital M, all 3 keys simultaneously) will indent a buffer, and
C-x h will highlight an entire file. Therefore,
C-x h C-M-\ will go through and properly indent an entire file, which can be very useful. If you want to comment an area you have highlighted use
C-c;, and to uncomment a highlighted area use
This is useful for running processes on the cluster. It allows you to log in, start your process, and then log off while your process continues to run until it completes.
Thanks to Josh and Mark for submitting their methods for background processes. The simplest method is to type from a terminal:
unset DISPLAY nohup matlab<anyfile.m>&outputfile&
Normally, when one logs out, all of the foreground and background processes are stopped, so in order to log out, without interupting the process, the command nohup is used. This alone automatically increases the priority number by 5 (1 is the highest priority 20 the lowest), but the nicest thing to do is to run nice together with nohup, to increase the priority number. The default for the nice command is 10. The & signal at the end puts the process in the background. You may be familiar with the & symbol at the end of a command already from running netscape or xemacs from the command line so that it starts its own window. This is also a background process, because the command line is returned to you immediately, and you can continue using the xterm while the other process is running. The meaning of 'unset DISPLAY' may now be coming clear to you. We use it in order to run a process in the background without it attempting to display a window. The less than sign means that the input comes from the file 'anyfile.m'. The greater than sign writes the output to file 'outputfile'. This file will be overwritten if it already exists, but if you use two greater than signs (anyfile.m>>&outputfile), it will be appended to the file instead. The first & directs the standard errors to the outputfile in addition to the standard output.
While we are talking about nice, if you find an interactive command is executing for a long time, and you did not nice it, you can suspend it and it and modify it with "nice".
ps aux |grep myprog
Which gives you an output something like this:
27654 pts/0 00:00:22 myprog
The number on the left is the PID and you use this to renice:
renice -p 27654
Create a file "commands" that contains:
unset DISPLAY matlab << end_tag matlab command 1 matlab command 2 ... etc quit end_tag
now change the permission, which makes it executable, indicating it's a shell script, not just a text file:
chmod u+x commands
run the program, setting nice:
nohup nice commands<>& outputfile &
Thanks to Jochen for discovering the following helpful hints: The default driver is "Postscript Level 2". Using it I got some partially colored printouts (not B&W!). Setting the driver to "Postscript Level 2 Color" fixed that. The simple rendering engine also seems to have some problems. I had some weird lines in my printout. Setting the rendering engine to "Open GL" in the printer driver options fixed that.