23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3 Basic Program Components<br />

X = -pi:pi/10:pi;<br />

Y = tan(sin(X)) - sin(tan(X));<br />

C{1,1} = 'LineWidth'; C{2,1} = 2;<br />

C{1,2} = 'MarkerEdgeColor'; C{2,2} = 'k';<br />

C{1,3} = 'MarkerFaceColor'; C{2,3} = 'g';<br />

plot(X, Y, '--rs', C{:})<br />

Function Return Values<br />

<strong>MATLAB</strong> functions can also return more than one value to the caller. These<br />

values are returned in a list with each value separated by a comma. Instead<br />

of listing each return value, you can use a comma-separated list with a<br />

structure or cell array. This becomes more useful for those functions that have<br />

variable numbers of return values.<br />

This example returns four values to a cell array:<br />

C = cell(1, 4);<br />

[C{:}] = fileparts('work/mytests/strArrays.mat')<br />

C =<br />

'work/mytests' 'strArrays' '.mat' ''<br />

Fast Fourier Transform Example<br />

The fftshift function swaps the left and right halves of each dimension<br />

of an array. For a simple vector such as [0 2 4 6 8 10] the output would<br />

be [6 8 10 0 2 4]. For a multidimensional array, fftshift performs this<br />

swap along each dimension.<br />

fftshift uses vectors of indices to perform the swap. For the vector shown<br />

above, the index [1 2 3 4 5 6] is rearranged to form a new index [4 5 6 1<br />

2 3]. The function then uses this index vector to reposition the elements. For<br />

a multidimensional array, fftshift must construct an index vector for each<br />

dimension. A comma-separated listmakesthistaskmuchsimpler.<br />

Here is the fftshift function:<br />

function y = fftshift(x)<br />

3-86

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!