A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.

Class

Class

Friday, April 15, 2016

Data Classes in MATLAB

Fundamental Data Types

All data in MATLAB is in the form of a matrix or array. But the data that we use in MATLAB software are of different classes. We can make matrices and arrays of any types of these data classes. Two of the MATLAB classes, structures and cell arrays, provide a way to store dissimilar types of data in the same array. There are 15 fundamental data classes in MATLAB. We can develop our own data types. Different data types require different amounts of storage space, the smallest type is ‘logical’, which requires only 1 byte.

(Picture courtesy: www.mathworks.com)

By default, MATLAB stores all numeric variables as double-precision floating-point values and text as characters.

Numeric Data Types

Numeric data types are mainly of two types: Floating-Point Numbers and Integers. Floating point numbers are again two types:
Double – This is the default numeric data type. It uses double precision. It can be created by simple assignment operator. ‘double( )’ command is used to convert other data types to double class.
Single – Any value stored as a single requires 32 bits. ‘single( )’ command is used to convert the double type data to single data type. Requires less storage space than double.
 ‘whos’ command can be used to display the attributes including class of a variable.
‘isfloat( )’ command is used to verify that a variable is a floating-point number. It returns logical 1 if the number is a floating-point number and logical 0 otherwise.
Example:
>> a=25.456
a =
   25.4560
>> whos
  Name      Size            Bytes  Class     Attributes
  a         1x1                 8  double             
>> b=single(a)
b =
   25.4560
>> whos b
  Name      Size            Bytes  Class     Attributes
  b              1x1                 4      single             

Note:
The range for double is:
   -1.79769e+308 to -2.22507e-308 and
    2.22507e-308 to  1.79769e+308
The range for single is:
            -3.40282e+38 to -1.17549e-38 and
             1.17549e-38 to  3.40282e+38

Integer – This class is used to represent the signed and unsigned whole numbers. Based on the size used (1, 2, 4 and 8 bytes), there are four types of signed and unsigned classes.
Class
Range of Values
Conversion Function
Signed 8-bit integer
-27 to 27-1
int8
Signed 16-bit integer
-215 to 215-1
int16
Signed 32-bit integer
-231 to 231-1
int32
Signed 64-bit integer
-263 to 263-1
int64
Unsigned 8-bit integer
0 to 28-1
uint8
Unsigned 16-bit integer
0 to 216-1
uint16
Unsigned 32-bit integer
0 to 232-1
uint32
Unsigned 64-bit integer
0 to 264-1
uint64

Example:
>> c=int16(-25.450)
c =
    -25
>> d=uint16(-25.450)
d =
      0
>> e=int64(-25.450)
e =
                  -25
>> whos
  Name      Size            Bytes  Class     Attributes
  c            1x1                 2        int16              
  d            1x1                 2       uint16             
  e            1x1                 8       int64              
>> x='Electrical'
x =
Electrical
>> y=int32(x)    % int command can be used to convert other data types to integers.
y =
          69         108         101          99         116         114         105          99          97         108 

Characters

MATALB store the text as character data type. Strings are represented as vectors of characters. MATLAB identifies  the characters in single quotation marks as text and such variables are saved as character data class.
‘char( )’ command is used to convert to character array. Convert the integers between 32–127 into a the printable ASCII characters.
‘ischar( )’ is used to Determine whether item is character array
>> x='Electrical';
>> whos x
  Name      Size            Bytes  Class     Attributes
  x              1x10               20    char
Considering the variable y from the previous example, we can convert it back to text using:
>> z=char(y)
z =
Electrical

Logical

The logical data type is used to store Boolean values, true or false states using the numbers 1 and 0, respectively. Output of some MATLAB functions or operators will be logical values, those variables are stored as logical data class.
‘logical( )’ - Convert numeric values to logical. Any nonzero element of A is converted to logical 1 (true) and zeros are converted to logical 0 (false).
‘islogical( )’ - Determine if input is logical array 

Tables

This data type is used to store the tables which have multiple columns of different data types. Tables consist of rows and column-oriented variables. Each variable in a table can have a different data type and a different size with the one restriction that each variable must have the same number of rows. The data may be stored or in a text file or in a spreadsheet.
Create table from workspace variables
Convert homogeneous array to table
Convert cell array to table
Convert structure array to table
Convert table to homogeneous array
Convert table to cell array
Convert table to structure array
Create table from file
Write table to file
Working with tables will be discussed in detail in another post.

Structures

Structure is a data type used to store information like a database. It groups related data using data containers called fields. Each field can contain data of any type or size. Access data in a structure using dot notation of the form structName.fieldName.
a = struct(field1 ,value1, field2 ,value2,…. ) is the syntax of command to create a structure with the specified field and values. The argument field is the name of the field and the value input argument can be any data type, such as a numeric, logical, character, or cell array.
Example:
>> field1 = 'voltage';
>> field2 = 'current';
>> value1 = [10, 20, 30];
>> value2 = [1, 2, 3];
>> vi = struct(field1, value1, field2, value2)
vi =
    voltage: [10 20 30]
    current: [1 2 3]

>> vi.voltage
ans =
    10    20    30
>> vi.current
ans =
     1     2     3
>> vi.resistance = 10
vi =
       voltage: [10 20 30]
       current: [1 2 3]
    resistance: 10 

Cell Array

A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. Cell arrays commonly contain either lists of text strings, combinations of text and numbers, or numeric arrays of different sizes.
An empty cell can be created using ‘cell( )’ command
>> C = cell(3,4)
C =
     [ ]     [ ]     [ ]     [ ]
     [ ]     [ ]     [ ]     [ ]
     [ ]     [ ]     [ ]     [ ]

A curly bracket{} operator can use the to create an empty 0-by-0 cell
>> C = {}
C =
     {}
>> D={[1 2 3]; 'text value'}
D =
    [1x3 double]
    'text value'
Refer to sets of cells by enclosing indices in smooth parentheses, (). Access the contents of cells by indexing with curly braces, {}. 

Function Handles

Function handles are the data type used to store variables that index to a function. Function handle used to construct anonymous functions or specify call back functions or call local functions from outside the main function.
To create a handle for a function, precede the function name with an @ sign.
>> magnitude = @abs;
>> magnitude(3+4i)
ans =
     5

submit to reddit

0 comments:

Post a Comment