This document provides an overview of a MATLAB programming course taught by Dr. A. Saksena at the National Rail Transport Institute in Vadodara, India on June 7, 2021. It covers MATLAB data types including numeric, logical, character, and special data types like cell arrays and structures. It describes how to work with complex numbers, strings, and conversions between different data types. Functions for determining data types and manipulating special data types like sparse matrices and cell arrays are also discussed. The document provides examples of creating and working with structures and using function handles in MATLAB.
This document provides an overview of a MATLAB programming course taught by Dr. A. Saksena at the National Rail Transport Institute in Vadodara, India on June 7, 2021. It covers MATLAB data types including numeric, logical, character, and special data types like cell arrays and structures. It describes how to work with complex numbers, strings, and conversions between different data types. Functions for determining data types and manipulating special data types like sparse matrices and cell arrays are also discussed. The document provides examples of creating and working with structures and using function handles in MATLAB.
This document provides an overview of a MATLAB programming course taught by Dr. A. Saksena at the National Rail Transport Institute in Vadodara, India on June 7, 2021. It covers MATLAB data types including numeric, logical, character, and special data types like cell arrays and structures. It describes how to work with complex numbers, strings, and conversions between different data types. Functions for determining data types and manipulating special data types like sparse matrices and cell arrays are also discussed. The document provides examples of creating and working with structures and using function handles in MATLAB.
7th June 2021 Taught by Dr. A. Saksena National Rail Transport Institute Vadodara MATLAB Data Types ● MATLAB does not require any type declaration or dimension statements. ● Whenever MATLAB encounters a new variable name, it creates the variable and allocates appropriate memory space. ● If the variable already exists, then MATLAB replaces the original content with new content and allocates new storage space, where necessary. ● Every data type stores data that is in the form of a matrix or array. The size of this matrix or array is a minimum of 0-by-0 and this can grow up to a matrix or array of any size. MATLAB Data Types List of Data Types List of Data Types
Sr.No. Data Type Description
1 int8 8-bit signed integer 2 uint8 8-bit unsigned integer 3 int16 16-bit signed integer 4 uint16 16-bit unsigned integer 5 int32 32-bit signed integer 6 uint32 32-bit unsigned integer List of Data Types
Sr.No. Data Type Description
7 int64 64-bit signed integer 8 uint64 64-bit unsigned integer 9 single single precision numerical data 10 double double precision numerical data 11 logical logical values of 1 or 0, represent true and false respectively List of Data Types
Sr.No. Data Type Description
12 Complex numbers with both a real and an imaginary component. 13 char character data (strings are stored as vector of characters) Complex Number Complex Number ● Most elementary mathematical functions are defined for complex values. ● These functions include exponential functions, logarithms, trigonometric functions, and square roots. ● The functions sin, cos, log, sqrt, and so forth will work as well with complex data as they will with real data. String ● MATLAB string is an array of type char. ● Each character is stored in two bytes of memory. ● A character variable is automatically created when a string is assigned to it. Data Type Conversions Data Type Conversions
S. No. Function Purpose
1 char Convert to character array (string)
2 int2str Convert integer data to string
3 mat2str Convert matrix to string
4 num2str Convert number to string
5 str2double Convert string to double-precision value
6 str2num Convert string to number
Data Type Conversions
S. No. Function Purpose
7 native2unicode Convert numeric bytes to Unicode characters 8 unicode2native Convert Unicode characters to numeric bytes 9 base2dec Convert base N number string to decimal number
10 bin2dec Convert binary number string to decimal number
11 dec2base Convert decimal to base N number in string
12 dec2bin Convert decimal to binary number in string
Data Type Conversions
S. No. Function Purpose
13 dec2hex Convert decimal to hexadecimal number in string
14 hex2dec Convert hexadecimal number string to decimal
number 15 hex2num Convert hexadecimal number string to double-precision number 16 num2hex Convert singles and doubles to IEEE hexadecimal strings Determination of Data Types Determination of Data Types
S. No. Function Purpose
1 isa Determine if input is object of specified class
2 iscellstr Determine whether input is cell array of strings
3 ischar Determine whether item is character array
4 isfloat Determine if input is floating-point array
5 isinteger Determine if input is integer array
6 islogical Determine if input is logical array
Determination of Data Types
S. No. Function Purpose
7 isnumeric Determine if input is numeric array
8 isreal Check if input is real array
9 isscalar Determine whether input is scalar
10 isstr Determine whether input is character array
11 isvector Determine whether input is vector
12 whos List variables in workspace, with sizes and types
Data Type Check Determination of types in String String Manipulation Function String Manipulation Function String Manipulation Function String Manipulation Function List of Special Data Types
Sr.No. Data Type Description
1 cell array array of indexed cells, each capable of storing an array of a different dimension and data type 2 structure C-like structures, each structure having named fields capable of storing an array of a different dimension and data type 3 function handle pointer to a function 4 user classes objects constructed from a user-defined class Special Data Type Conversions
S. No. Function Purpose
1 mat2cell Convert array to cell array with potentially different sized cells
2 num2cell Convert array to cell array with consistently sized cells
3 struct2cell Convert structure to cell array
4 cell2mat Convert cell array to numeric array
5 cell2struct Convert cell array to structure array
6 cellstr Create cell array of strings from character array Determination of Data Types
S. No. Function Purpose
1 iscell Determine whether input is cell array
2 isfield Determine whether input is structure array field
3 isobject Determine if input is MATLAB object
4 isstruct Determine whether input is structure array
5 class Determine class of object
6 validateattributes Check validity of array
Cell Arrays Cell Arrays Preallocating Cell Arrays Example Common Cell Functions Sparse Matrices ● A sparse matrix is a large matrix in which the vast majority of the elements are zero. ● Storing and working with large matrices, most of whose elements are zero, is a serious waste of both computer memory and CPU time. ● Unfortunately, many real-world problems naturally create sparse matrices, so we need some efficient way to solve problems involving them. ● MATLAB has a special version of the double data type that is designed to work with sparse arrays. ● In this special version of the double data type, only the nonzero elements of an array are allocated memory locations, and the array is said to have the “sparse” attribute. Sparse Attributes ● An array with the sparse attribute actually saves three values for each nonzero element: the value of the element itself and the row and column numbers where the element is located. ● Even though three values must be saved for each nonzero element, this approach is much more memory efficient than allocating full arrays if a matrix has only a few nonzero elements. Example Common Sparse Matrix Functions Common Sparse Matrix Functions Structures ● An array is a data type in which there is a name for the whole data structure, but individual elements within the array are known only by number. All elements of array are of same type. ● A cell array is a data type in which there is a name for the whole data structure, but individual elements within the array are known only by number. However, the individual elements in the cell array may be of different types. ● In contrast, a structure is a data type in which each individual element has a name. The individual elements of a structure are known as fields, and each field in a structure may have a different type. The individual fields are addressed by combining the name of the structure with the name of the field, separated by a period. ● A structure can be build one field at a time using assignment statements. Each time data is assigned to a field, that field is automatically created. Structures Structures ● A second student can be added to the structure by adding a subscript to the structure name ● Note that all of the fields of a structure are created for each array element whenever that element is defined, even if they are not initialized. ● The uninitialized fields will contain empty arrays, which can be initialized with assignment statements at a later time. Structures Structures Adding Field from Structure Removing Field from Structure Get Field Set Field Nesting Structure Function Handles Function Handles Calling Function using Function Handles Calling Function using Function Handles Function Evaluation Advantages of Function Handle ● Passing Function Access Information to Other Functions. ● Improved Performance in Repeated Operations. ● Allow Wider Access to Subfunctions and Private Functions. ● Include More Functions per M-File for Easier File Management. ● Each function handle created for a function has its own independent workspace. If we create two different handles for this function, each one will have its own local data, and they will be independent of each other. Future Discussions File read write