Software Docs


Browse CVS, Git, or SVN
Software Repositories
OS Security Updates
LIGO software virtual machine
VMware SL6 Install


SCCB - Software Change Control Board
Edit these pages
Mailing List


LDAS Tools
LDG Client/Server
LVAlert Administration
NDS Client

Legacy Projects


LSC MATLAB Applications


Tips and Tricks in MATLAB


  • To access parts of structures or structure arrays as vectors, you may need to use an intermediate variable. Here is an example:
    A(1).B.C = 11;
    A(2).B.C = 22;
    A(3).B.C = 33;
    temp = [A.B];
    vector = [temp.C]
    vector =
        11    22    33
  • You can not use the 'varargin' from the input parameter list within a function to fill an input parameter list when calling another function
  • Within a structure, fields are added in the order that they are encountered in the code. If you later wish to refer to structure fields by number, then all paths should define the fields in the same order.
  • You can force a vector into column or row vector format. Use c = x(:); to create a column-only vector and r = x(:)'; to create a row vector out of arbitrary vector x.
  • Remember that MATLAB does not actually pass-by-value: it passes a copy-on-write pointer. If you don't try to write to the passed argument no copy is made and no additional memory is used. If, on the other hand, you do write to the argument, then a copy is created.
  • MATLAB does not actually change an existing object using 'set'. Instead it returns a new object which is a copy of the old object with the change. Thus, calls to 'set' need to use the returned object. That is, use
     myObject = set(myObject,'examField',12); 
  • Note that you can read environment variables using the 'getenv' function but there is no mechanism to set environment variables within MATLAB. Even if you shell out within MATLAB to do this, the new environment variables are not seen by the MATLAB session.

    License Management

  • You can find out who is using which MATLAB licences by using 'lmstat'. This utility is located in the 'etc' sub-directory of your MATLAB installation directory. Thus, if your current MATLAB is in /usr/local/matlab, then type the following at the command prompt:
     /usr/local/matlab/etc/lmstat -a
  • Note that MATLAB toolbox licenses stay assigned (once used) as long as the MATLAB session stays active. Thus, you should try to avoid keeping an interactive MATLAB session running overnight. It also takes about 30 minutes for a MATLAB toolbox license to become free.
  • MATLAB versions

  • In MATLAB release 14, characters are stored in Unicode format. This makes normal Release 14 workspace files ('*.mat') unreadable by MATLAB Release 13. To force workspace files to be stored in a format readable by Release 13, use the '-V6' switch. Here is an example:
     save mywork.mat -V6; 
    The 'V6' option is because MATLAB Release 13 is also Version 6, and MATLAB Release 14 is Version 7.
  • Tips on Compiled MATLAB

  • Since command-line arguments are passed to a MATLAB executable as character strings, special care must be taken when passing numeric data. This is because when a function is called in interactive MATLAB, numbers are still passed as numbers. A simple way to handle this is to add an ischar() function call to test the input, and then convert it using str2num. Here is an example:
    function testcall(numInput,charInput)
        numInput = str2num(numInput);
    fprintf('The inputs were number %f and string %s\n',numInput,charInput);
    This allows the same code to be used in both compiled and interactive MATLAB. The str2num function will also convert vector forms such as '[1:7]'. The matapps CVS has a utility function (strassign) which does the conversion and also handles the passing of arrays of character strings.
  • There is an odd bug in the implementation of the 'saveas' function. To use this function in a compiled executable, the environment variable ARCH must be set to the MATLAB architecture. Use glnx86 for Linux. Note that interactive MATLAB defines this within the interactive MATLAB session.
  • Note that at least with MATLAB Compiler 4 (used with Release 14), you can make calls that create MATLAB figures, etc. without having an X-Windows buffer defined. Thus, you can create graphics with MATLAB executables on grid computers. See the above note on a related bug.
  • For unclear reasons, MATLAB Compiler 4 will not include the System Identification toolbox when creating the *.ctf file. However, if you copy the required source code directories of the toolbox (typically ident and idutils) to a user directory and use 'addpath' to point to them, you can build executables using them
  • MATLAB Cluster Tips

  • The MATLAB Compiler 4 (used with Release 14) creates two files: an executable and a *.ctf file. The *.ctf file is expanded at run time into a *_mcr directory with byte-code files that are interpreted by the executable. If more than one executable is run (say, on a cluster), the first executable will start to expand the *.ctf file. The second executable will look and see a *_mcr directory and think that it does not need to expand the *.ctf file. It may start looking for files in the *_mcr directory that are not there yet. This can be a problem. The current work around is to run a copy of the executable once before you submit a lot of your jobs. The executable doesn't even need to have proper arguments, it just needs to run. You should also remove the old *_mcr directory after changing the executable and *.ctf files. An alternative is to copy the executable and .ctf file to a scratch directory that is unique to each job and run it from there.
  • There is a problem with code compiled with Matlab 7 (compiler 4) if the the HOME environment variable is not set. A workaround is to add a line to the Condor submit file that sets the variable (It is also a good idea to set the LD_LIBRARY_PATH at the same time) eg. add a line to your condor submit file like:
    environment = HOME=/dso-test/charlton;LD_LIBRARY_PATH=/ldcg/matlab_r14_sp2/bin/glnx86:/ldcg/matlab_r14_sp2/extern/lib/glnx86:/ldcg/matlab_r14_sp2/sys/os/glnx86
  • $Id$