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.

1 Data Structures<br />

This example runs a user-defined function compareResults on the data in<br />

matrices stats04 and stats03. Each time through the loop, it concatenates<br />

the results of this function onto the end of the data stored in comp04:<br />

col = 10;<br />

comp04 = [];<br />

for k = 1:50<br />

t = compareResults(stats04(k,1:col), stats03(k,1:col));<br />

comp04 = [comp04; t];<br />

end<br />

Concatenating to a Structure or Cell Array. Youcanaddontoarraysof<br />

structures or cells in the same way as you do with ordinary matrices. This<br />

example creates a 3-by-8 matrix of structures S, eachhaving3fields:x, y, and<br />

z, and then concatenates a second structure matrix S2 onto the original:<br />

Create a 3-by-8 structure array S:<br />

for k = 1:24<br />

S(k) = struct('x', 10*k, 'y', 10*k+1, 'z', 10*k+2);<br />

end<br />

S = reshape(S, 3, 8);<br />

Create a second array that is 3-by-2 and uses the same field names:<br />

for k = 25:30<br />

S2(k-24) = struct('x', 10*k, 'y', 10*k+1, 'z', 10*k+2);<br />

end<br />

S2= reshape(S2, 3, 2);<br />

Concatenate S2 onto S along the horizontal dimension:<br />

S = [S S2]<br />

S =<br />

3x10 struct array with fields:<br />

x<br />

y<br />

z<br />

1-28

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

Saved successfully!

Ooh no, something went wrong!