% SECTION E: ADVANCED DATA TYPES IN MATLAB % STRUCTURE ARRAYS AND CELL ARRAYS a=10; b= [ 1 2 ; 3 4] b = 1 2 3 4 c= 'alexg' c = alexg whos Name Size Bytes Class Attributes a 1x1 8 double ans 1x20 40 char b 2x2 32 double c 1x5 10 char z= whos % z is a structure array that is a column vector of 4 elements. % Every element of z is a collection of variable data types. Each data types % has a name. You can address/retrieve its value with .field-name where % for z field-name is one of name, size, etc. Thus for the third element % z(3).name gives the name of the third variable reported by whos i.e. b % z = 4x1 struct array with fields: name size bytes class global sparse complex nesting persistent z(1) ans = name: 'a' size: [1 1] bytes: 8 class: 'double' global: 0 sparse: 0 complex: 0 nesting: [1x1 struct] persistent: 0 z(1).name ans = a % Here is the example mentioned in the introduction of section E. z(3).name ans = b whos Name Size Bytes Class Attributes a 1x1 8 double ans 1x1 2 char b 2x2 32 double c 1x5 10 char z 4x1 6212 struct z(2).name ans = ans z(3).size ans = 2 2 z(3).size(1) ans = 2 z(3).size(2) ans = 2 z(4).size(2) ans = 5 z(4).size(1) ans = 1 % ANOTHER WAY TO RETRIEVE THIS INFORMATION IS getfield(zz, {4},'size',{1}) % which is equivalent to z(4),size(1) %setfield gets one extra argument, last one and that is the value assigned ! % % zz(3).name returns the name of third variables % but % eval(zz(3).name) returns its value instead! z z = 4x1 struct array with fields: name size bytes class global sparse complex nesting persistent z(:).size ans = 1 1 ans = 1 20 ans = 2 2 ans = 1 5 z(:) ans = 4x1 struct array with fields: name size bytes class global sparse complex nesting persistent % SECTION F: CELL ARRAYS % % cell arrays are matrices. Whereas matrices/ordinary arrays store a collection % of elements with the same data type, cell arrays can hold arbitrary data types. % For cell array element indexing using { } braces not () parentheses as % is zz{1,2} vs zz(1,2) for the element of first row and second column % % creates a 5x5 cell array cell(m,n) is also possible... % % zz= cell(5) zz = [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] % the 4,4 element is a scalar zz{4,4}= 10 zz = [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [10] [] [] [] [] [] [] % the 1,2 element is a 2x2 array of doubles. zz{1,2} = [ 4 5 ; 7 8] zz = [] [2x2 double] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [10] [] [] [] [] [] [] % the 5,5 elements is a 10x1 vector of int8 integers % % zz{5,5}= ones(10,1,'int8'); zz zz = [] [2x2 double] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [10] [] [] [] [] [] [10x1 int8] % To print the values stored in the various locations do a celldisp(zz); % to get a printout of the geometry and structure of cell arrays do a cellplot(z); % One more example a = 10 a = 10 b= [10 ; 12 ; 13] b = 10 12 13 c= 'alexg' c = alexg d = ones(4,4); zz = whos % zz is a structure array that is a column vector of 5 elements. % Every element of z is a collection of variable data types. Each data types % has a name. You can address/retrieve its value with .field-name where % for z field-name is one of name, size, etc. Thus for the third element % zz(3).name gives the name of the third variable reported by whos i.e. b % zz = 5x1 struct array with fields: name size bytes class global sparse complex nesting persistent zz zz = 5x1 struct array with fields: name size bytes class global sparse complex nesting persistent whos Name Size Bytes Class Attributes a 1x1 8 double ans 1x20 40 char b 3x1 24 double c 1x5 10 char d 4x4 128 double zz 5x1 7622 struct zz(1) ans = name: 'a' size: [1 1] bytes: 8 class: 'double' global: 0 sparse: 0 complex: 0 nesting: [1x1 struct] persistent: 0 zz(4).bytes ans = 10 % c is the 4th variable defined and retrieved by whos, after a,ans,b zz(4).name ans = c eval(zz(4).name) c = alexg % geometry of variable c ; number of columns zz(4).size(2) ans = 5 % ANOTHER WAY TO RETRIEVE THIS INFORMATION IS getfield(zz, {4},'size',{1}) % which is equivalent to z(4),size(1) %setfield gets one extra argument, last one and that is the value assigned ! % % zz(3).name returns the name of third variables % but % eval(zz(3).name) returns its value instead! % SECTION F: CELL ARRAYS % % cell arrays are matrices. Whereas matrices/ordinary arrays store a collection % of elements with the same data type, cell arrays can hold arbitrary data types. % For cell array element indexing using { } braces not () parentheses as % is qq{1,2} vs qq(1,2) for the element of first row and second column % % creates a 5x5 cell array cell(m,n) is also possible... % % qq= cell(5) qq = [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] qq{1,1} = ones(4,1); qq{4,4} = 10; qq{5,5} = zeros(3,1); qq = [4x1 double] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [10] [] [] [] [] [] [3x1 double] qq{1,1} ans = 1 1 1 1 celldisp(qq) qq{1,1} = 1 1 1 1 qq{2,1} = [] qq{3,1} = [] qq{4,1} = [] qq{5,1} = [] qq{1,2} = [] qq{2,2} = [] qq{3,2} = [] qq{4,2} = [] qq{5,2} = [] qq{1,3} = [] qq{2,3} = [] qq{3,3} = [] qq{4,3} = [] qq{5,3} = [] qq{1,4} = [] qq{2,4} = [] qq{3,4} = [] qq{4,4} = 10 qq{5,4} = [] qq{1,5} = [] qq{2,5} = [] qq{3,5} = [] qq{4,5} = [] qq{5,5} = 0 0 0 cellplot(qq)