## MATLAB – Interrupting function execution

by on Mar.02, 2007, under How To ..., Software

MATLAB itself does not provide any way of arbitrarily interrupting/pausing a function at mid-execution to examine the internal function stack/variables. It is a pity since such a mechanism would be an extremely valuable debugging tool.

Finally, I realized that this is not as impossible as it seems. In fact, it is EXTREMELY simple! All that is required are four lines of code in any script/function:

[statcode, result] = system('ls sometoken');
if statcode == 0
keyboard
end


The system command is used to run system console commands. Here, a simple check is made for the existence of the file ‘sometoken’ in the current directory. If the file exists, statcode is set to zero and the if condition is satisfied. The keyboard command is then executed which causes MATLAB to pause execution and return control to the command line! In order to continue execution, ‘sometoken’ must be deleted/removed from the current directory and the command return must be issued.

The best place for this code is perhaps in any loop that might exist in the MATLAB script/function. Then, to cause execution to pause, one simply needs to create a file ‘sometoken’ in the current directory.

## Tesselated Cells

by on Feb.03, 2006, under How To ..., Software

Finally .. a script (link broken since 2007) that will generate a square field of tesselated cells in MATLAB. The script take as input the desired cell radius and the dimension of the square field.

Generating a cell is simple enough. The corners are simply R*exp(j*(-pi:pi/3:pi)+pi/6)/cos(pi/6). The additional pi/6 is needed to rotate the resulting hexagon. The real challenge is in the tesselation. In order to achieve that, two base vectors that govern the tesselation must be defined:

vi = 2*R;
vj = 2*R*(cos(pi/3) + j*sin(pi/3))


Using these base vectors, the centers of every cell in the tesselation can be defined:

for a = 1:x
for b = 1:x
centr(a,b) = a*vi+b*vj;
end
end


One we have the centers, use the MATLAB repmat function to replicate the corners around each of the centers to get the tesselated cells:

cells = repmat(corners.', 1,...
numel(centr))+repmat(reshape(centr.',1,...
numel(centr)),length(corners), 1);


A plot of the cells can be achieved using:

plot(real(cells),imag(cells));


This will however, generate a tesselation that is skewed. The linked *.m file takes care of that issue to generate a square tesselated field.

## PSfrag for EPS Graphichs Text Manipulation

by on Jan.17, 2006, under How To ..., LaTeX, Software

There’s a nice package called psfrag that allows you to insert LaTeX constructs into EPS figures. This is specially useful with EPS files saved from MATLAB plots. The way it works is by replacing a given tag in the text of the EPS file with the LaTeX construct.

E.g. label the x-axis of of the plot as XLABEL and save the plot as an EPS file. Then, when you include that file, just put in the \psfrag{}{} tag:

\psfrag{XLABEL}{$\frac{\tau}{\sigma}$}\includegraphics{file}

The most obvious disadvantage is that it only works with EPS figures — so no PdfLaTeX. So, to compile a document to PDF, you’ll need to go the old latex -> dvi2ps -> ps2pdf way.

More details can be found on CTAN.

## MATLAB 7 SP2 on FC4 (x86_64)

by on Aug.25, 2005, under How To ..., Linux, Software

The installer will not run on a default install. To get that working, I needed to install the xorg-x11-deprecated-libs.x86_64 package. It appears to contain some libraries needed by the graphical installer. Once installed, the 64bit version spits out the following error (the 32bit install is fine):

*** glibc detected *** malloc(): memory corruption: 0x0000000000598840 ***


The fix is:

$LD_ASSUME_KERNEL=2.4.1$ export LD_ASSUME_KERNEL


It’s best to put these lines at the top of $MATLAB/bin/.matlab7rc.sh (where$MATLAB is the installation directory).

Got this little hint from the MATLAB Support site.