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.

9 Classes and Objects<br />

A(1,2).name(3:4)<br />

calls subsref(A,S), where S is a 3-by-1 structure array with the values:<br />

S(1).type = '()' S(2).type = '.' S(3).type = '()'<br />

S(1).subs = {1,2} S(2).subs = 'name' S(3).subs = {3:4}<br />

How to Write subsref<br />

The subsref method must interpret the subscripting expressions passed in by<br />

<strong>MATLAB</strong>. A typical approach is to use the switch statement to determine the<br />

type of indexing used and to obtain the actual indices. The following three<br />

code fragments illustrate how to interpret the input arguments. In each case,<br />

the function must return the value B.<br />

For an array index:<br />

switch S.type<br />

case '()'<br />

B = A(S.subs{:});<br />

end<br />

For a cell array:<br />

switch S.type<br />

case '{}'<br />

B = A(S.subs{:});<br />

end<br />

% A is a cell array<br />

For a structure array:<br />

switch S.type<br />

case '.'<br />

switch S.subs<br />

case 'field1'<br />

B = A.field1;<br />

case 'field2'<br />

B = A.field2;<br />

end<br />

end<br />

9-18

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

Saved successfully!

Ooh no, something went wrong!