% This m-file is an introduction to more advanced data structures clear % clear all variables and start fresh %% Multidimensional matrices A = rand(2,4,3,3) % A [2x4x3x3] matrix %% A single structure called WinterTerm with three fields WinterTerm.course = 'Math 2T03'; WinterTerm.prof = 'Kevlahan'; WinterTerm.marks = [80 75 95 2 73 62 93]; WinterTerm pause whos WinterTerm pause % Now add more records WinterTerm(2).course = 'Math 2C03' ; WinterTerm(3).course = 'Math 2S03'; WinterTerm(2).prof = 'Wolkowicz' ; WinterTerm(3).prof = 'Boden'; WinterTerm(2).marks = [23 40 15]; WinterTerm(3).marks = [95 90 85 83 75]; whos WinterTerm pause WinterTerm(2).marks(3) % 3rd mark from 2nd course WinterTerm(3).marks % All marks %% Creating a structure array using the struct function clear WinterTerm WinterTerm = [struct('course','Math 2T03','prof','Kevlahan','marks', [80 75 95 2 73 62 93]); struct('course','Math 2C03','prof','Wolkowicz','marks',[23 40 15]); struct('course','Math 2S03','prof','Boden','marks',[95 90 85 83 75])]; WinterTerm.marks %% A Cell is an array of data containers % It is the most versatile data object it can even contain structures! C = cell(2,2) % Create a 2x2 cell C{1,1} = rand(3) % Put a 3x3 random matrix in 1st box C{1,2} = char('john','raj'); % a string array in 2nd box C{2,1} = WinterTerm; % put a structure in the 3rd box C{2,2} = cell(3,3); % put a 3x3 cell in the 4th box % Create cell directly clear C C = {rand(3), char('john','raj'); WinterTerm cell(3,3)}; C % Gives types of contents the containers of C have C{1,2} % Use content indexing to access entire contents of container at location {i,j} pause C(1,2) % Use index notation to access a container, but not its contents pause C{1,2}(1,:) % Use multiple index notation to access a particular data in a particular container pause C{2,1}(3).prof % Combine structure notation with cell indices C{2,1}(3).marks(3:5) pause celldisp(C) pause cellplot(C)