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.

2 Data Types<br />

where the arguments are field names and their corresponding values. A field<br />

value can be a single value, represented by any <strong>MATLAB</strong> data construct, or<br />

acellarrayofvalues. Allfieldvaluesintheargumentlistmustbeofthe<br />

same scale (single value or cell array).<br />

You can use different methods for preallocating structure arrays. These<br />

methodsdifferinthewayinwhichthestructure fields are initialized. As an<br />

example, consider the allocation of a 1-by-3 structure array, weather, withthe<br />

structure fields temp and rainfall. Three different methods for allocating<br />

such an array are shown in this table.<br />

Method Syntax Initialization<br />

struct weather(3) = struct('temp', 72, ...<br />

'rainfall', 0.0);<br />

struct with<br />

repmat<br />

struct with cell<br />

array syntax<br />

weather = repmat(struct('temp', ...<br />

72, 'rainfall', 0.0), 1, 3);<br />

weather = ...<br />

struct('temp', {68, 80, 72}, ...<br />

'rainfall', {0.2, 0.4, 0.0});<br />

weather(3) is initialized with<br />

the field values shown. The<br />

fields for the other structures<br />

in the array, weather(1) and<br />

weather(2), are initialized to<br />

the empty matrix.<br />

All structures in the weather<br />

array are initialized using one<br />

set of field values.<br />

The structures in the weather<br />

array are initialized with<br />

distinct field values specified<br />

with cell arrays.<br />

Naming conventions for Structure Field Names<br />

<strong>MATLAB</strong> structure field names are required to follow the same rules as<br />

standard <strong>MATLAB</strong> variables:<br />

1 Field names must begin with a letter, which may be followed by any<br />

combination of letters, digits, and underscores. The following statements<br />

are all invalid:<br />

w = setfield(w, 'My.Score', 3);<br />

w = setfield(w, '1stScore', 3);<br />

w = setfield(w, '1+1=3', 3);<br />

2-78

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

Saved successfully!

Ooh no, something went wrong!