Tuesday, January 28, 2014

Linking Axes

When using multiple plots, especially subplots, it's possible to link the axes using the (you guessed it) linkaxes function. This function will link the axes so that the plots will move and zoom together, making it easier to compare the plots.

t = (0:(length(rc)-1))/fs;
x = sin(2*pi*1000*t);
y = sin(2*pi*1000*t + pi/4);


subplot(211); plot(t, x); 
ax(1) = gca; 
subplot(212); plot(t, y); 
ax(2) = gca; 
linkaxes(ax, 'xy'); 

Monday, January 6, 2014

Docking Matlab Figures


To dock the figure window in Matlab, create a file named startup.m in Matlab's default directory and add the following line.
set(0,'DefaultFigureWindowStyle','docked')

In case you don't know where the default directory is, open Matlab and enter the command pwd in the command window.

To revert the docking behavior, either change the line in startup.m to
set(0,'DefaultFigureWindowStyle','normal')

or delete startup.m.

Friday, January 3, 2014

Functional Programming in Matlab

It is possible to write functional programming style loops with arrayfun.

Let's start with an lambda function example.

>> y = arrayfun(@(x) x^2,1:10)

y =
     1     4     9    16    25    36    49    64    81   100 


We can also use the function handle @ with custom functions.

testFun.m
function y = testFun(x)

% do something more interesting...
if x > 0.5,
    y = 1;
else,
    y = 0;
end

Code to execute
x = rand(10, 1); % 10 random numbers between 0 and 1
y = arrayfun(@testFun, x);