only shadlen.org

HowTos / Matlab

Matlab



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 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.

Playing with Matlab

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.

The Matlab Gui

To start the matlab gui type matlab7 & in the xterm window. 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.

The Command Line

Very simple, open a terminal, and type matlab

Matlab from Xemacs

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:

  • syntax checking
  • syntax coloring
  • start matlab in a emacs-shell
  • run matlab code from within (X)Emacs
  • debugging
  • and much more

If you are not familiar with xemacs, I've thrown together some xemacs hints. There is also a good intro at called control-escape. Also check out the help manual by clicking on the info button.

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 C-c e.

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 C-c:.

Running processes in the background from matlab

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 inn 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.

More Nice

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".

  • start: /.myprog.exe
  • suspend: ^Z
  • discover PID: ps |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

Matlab Batch Jobs

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 &

Printing from Matlab

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.