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.

4 M-File <strong>Programming</strong><br />

City Temp. Heat Index Wind Speed Wind Chill<br />

Boston 43 32 8 37<br />

Chicago 34 27 3 30<br />

Lincoln 25 17 11 16<br />

Denver 15 -5 9 0<br />

Las Vegas 31 22 4 35<br />

San Francisco 52 47 18 42<br />

The information is stored in structure W. The structure has one field for each<br />

column of data:<br />

W = struct('city', {'Bos','Chi','Lin','Dnv','Vgs','SFr'}, ...<br />

'temp', {43, 34, 25, 15, 31, 52}, ...<br />

'heatix', {32, 27, 17, -5, 22, 47}, ...<br />

'wspeed', {8, 3, 11, 9, 4, 18}, ...<br />

'wchill', {37, 30, 16, 0, 35, 42});<br />

To update the data base, you can pass the entire structure, or just one<br />

field with its associated data. In the call shown here, W.wchill is a<br />

comma-separated list:<br />

updateStats(W.wchill);<br />

Passing Arguments in a Cell Array<br />

You can also group arguments into cell arrays. The advantage over structures<br />

is that cell arrays are referenced by index, allowing you to loop through a<br />

cell array and access each argument passed in or out of the function. The<br />

disadvantage is that you don’t have field names to describe each variable.<br />

This example passes several attribute-value arguments to the plot function:<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 />

4-64

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

Saved successfully!

Ooh no, something went wrong!