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 />

40<br />

Thisisthesameasexplicitlytyping<br />

S(1).f5, S(2).f5, S(3).f5, S(4).f5<br />

Assigning Output from a Comma-Separated List<br />

You can assign any or all consecutive elements of a comma-separated list to<br />

variables with a simple assignment statement. Using the cell array C from<br />

theprevioussection,assignthefirstrowtovariablesc1 through c6:<br />

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

for k = 1:24, C{k} = k * 2; end<br />

[c1 c2 c3 c4 c5 c6] = C{1,1:6};<br />

c5<br />

c5 =<br />

34<br />

If you specify fewer output variables than the number of outputs returned by<br />

the expression, <strong>MATLAB</strong> assigns the first N outputs to those N variables, and<br />

then discards any remaining outputs. In this next example, <strong>MATLAB</strong> assigns<br />

C{1,1:3} to the variables c1, c2, andc3, and then discards C{1,4:6}:<br />

[c1 c2 c3] = C{1,1:6};<br />

You can assign structure outputs in the same manner:<br />

S = cell2struct(C, {'f1', 'f2', 'f3', 'f4', 'f5', 'f6'}, 2);<br />

[sf1 sf2 sf3] = S.f5;<br />

sf3<br />

sf3 =<br />

38<br />

Youalsocanusethedeal function for this purpose.<br />

3-82

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

Saved successfully!

Ooh no, something went wrong!