function matlab_types % For a list of datatypes, type 'help class' % Most variables are created as type 'double' %% BASICS a = 3 class(a) % An array is treated as whatever data type is inside, in this case double disp_type( 3:7 ) % function handle disp_type( @matlab_types ) % cell array disp_type( cell(2,2) ) % strings are arrays of characters b = 'MATLAB rules' disp( b(8:end) ) disp_type( b ) % Changing the type of an object rarely causes a warning % This means you should be careful about overwriting data! a = 3:20 % No warning, you probably meant to do this a.best_field = 'hello' % MATLAB thinks you probably didn't want to do this disp_type( a ) %% CELL ARRAYS % Cell arrays are multidimensional arrays that hold arbitrary data a = cell(2) % EQUIVALENT: a{2,2} = []; a{1,1} = 'Character array'; a{1,2}.new_field = 'awesome'; a{2,1} = [1 2; 3 4]; a{2,2} = a %% STRUCT ARRAYS clear a a(3).height = 20; % Preallocation is specifying the largest element first a(1).weight = 30; a(1).name = 'tim'; isfield(a, 'age') isfield(a, 'height') a(2).name = 'alex'; a(3).name = 2; % A different type from a(1).name; it's allowed, but be careful a(3).name = 'jasmin' [a(3)] % Use brackets to pull information from fields across the struct array {a(3)} % Or use braces to get the across-struct field in a cell array % Multidimensional struct arrays are allowed, just like arrays and cell arrays a(3,2,2,2).height = 25; %% function disp_type(a) % This function can't normally be accessed from outside the m_file class_a = class(a); header = '%%------%%'; disp(sprintf('%s\rData is:', header)) % sprintf behaves somewhat like its C disp(a) % analog disp( sprintf('Type is:\r %s\r\r', class_a) )