Friday, July 17, 2015

Default function parameter setting in Matlab


if ~exist('param', 'var'),
    param = defaultParam;
end

Note that 'param' in the first line is in single quotes. It's a string, not the parameter itself.
This allows for reordering of parameters unlike the case of checking nargin.

Monday, April 20, 2015

Shell Script for running Matlab

This script can be used to run multiple instances of Matlab on OS X.

In /usr/local/bin/matlab,
for i in $*;
do
    params=" $params $d$i"
done

sudo /Applications/MATLAB_R20XXx_Student.app/bin/matlab $params

where /Applications/MATLAB_R20XXx_Student.app/bin/matlab should be your Matlab executable.

Friday, March 27, 2015

Flipping the Y axes in Matlab

In most computer graphics APIs/toolkits, the default Y direction is from top to bottom, i.e. the Y origin (y = 0) is the top and increases as it goes downward. However in most scientific plots, the Y direction is the opposite. The positive Y direction is upwards.
Because of this discrepancy, there is always confusion when drawing images, especially in Matlab (e.g. imagesc). While one possibility would be to flip the data array via flipud, my preferred method is to use the 'ydir' option.

set(gca, 'ydir', 'normal');
This leaves the data as is, no flipping data, and only changes the the plot options, which separates data from presentation. (Model/View)
An interesting point is that the default value for ydir is 'reverse' which is the default direction of most graphics packages.

Wednesday, March 11, 2015

Timestamps in Matlab

Matlab has a nifty function for taking timestamps to measure the time it takes to run something. The functions are tic and toc.
You start a timer by calling tic then stop it by calling toc. When toc isn't assigned to a variable, it will print a timestamp summary to the console.
You can save a tic_id to use multiple timers.
Here is a simple example that uses the timer ID:

t_id = tic;
your_time_consuming_function();
toc(t_id)

Monday, November 3, 2014

Matlab 2011a on OS X 10.10 Yosemite

I have been able to run Matlab 2011a (Student Version) on Yosemite following the steps below.
The main problem seems to stem from the lack of Java support for the new look-and-feel.


1. Update Java and XQuartz

This may not be a critical component, but keeping Java and XQuartz up-to-date is probably good practice.

Java for OSX 2014-001: http://support.apple.com/kb/dl1572
XQuartz: https://xquartz.macosforge.org/


2. Patch Matlab

Mathworks has released patches for versions 2011a to 2014a.
http://www.mathworks.com/support/bugreports/1098655
The patch will add (compiled Java) files to the app bundle under MATLAB_your_version.app/java/patch/
Without the patch you will randomly get Java null pointer errors caused by missing Aqua look-and-feel(laf) files (files for the old OS X Aqua UI). This will prevent the help dialog from opening.

By now Matlab will run when launched by terminal command line. Trying to run Matlab by clicking the application icon or from a Spotlight search will result in the following error message.


3. Patch Info.plist

Open /Applications/MATLAB_your_version.app/Contents/Info.plist in your text editor of choice. You will need to open it as administrator (using sudo).
The file is an XML file. Find CFBundleVersion. The value for CFBundleVersion will be something like 2.1. Change it to 9.1.
I am not 100% sure what this hack does, but from the looks, it seems like it is bypassing a version check.
By now, everything should be working.

And some thoughts...

Despite an open beta program for OS X 10.10 Yosemite, during which Mathworks seems to have been preparing Matlab 2014b, they apparently did not test the compatibility of previous versions of Matlab with the new OS.
Throughout the beta period and into the first couple of weeks of Yosemite's release, there has been a lot of confusion regarding which versions of Matlab would be supported on Yosemite, especially with versions prior to 2013a. After a period of confusion on Mathworks part, it seems they have settled to support versions 2011a or later.

Given most undergraduate programs last on average 4 years (being a student user myself), I personally think each Matlab version should be supported at least 4 years. Especially when "supporting a release" means patching some missing files and properly reading the OS version. It makes little sense for the relatively expensive software package to have such a randomly short support schedule.

All in all, this may be a good time to move to open source alternatives such as R or Python.

Monday, September 22, 2014

Drop-In Gallery

Have a folder of images on a server running PHP?
Just drop-in a file to make it an online gallery!

Details here.
Or download the code here.

Envelope Detector for Modulated Signals

The following line of code will find the envelope of a modulated signal in Matlab.

env = abs(hilbert(x));

Here is some test code:

% create modulated signal
fc = 100;
fm = 17;
fs = 8000;
t = 0:1/fs:1;
x = sin(2*pi*fc*t) .* (1.5+sin(2*pi*fm*t));

plot(t, x);

%% get modulation envelope
env = abs(hilbert(x));
hold on;
plot(t, env, 'r');
hold off;