Saturday, February 14, 2015

MATLAB TuTeR 6: Data types with MATLAB



Data types with MATLAB

Matlab constants:


inf = means infinity number. e.g. 1/0

isinf(x) = means is x is infinity? if yes, returns 1. if no, returns 0.

nan = means not a number. e.g. 0/0, inf/inf

isnan(x) = means is x is not a number? if yes, returns 1. if no, returns 0.

pi = means pi 3.14.....

i & j = represents complex number 0+1i which equal sqrt(-1(, the complex numbers has 16 bytes double number which attribute is complex.

 

Matlab data types or Classes

class(x) = returns the data type of x, e.g. double, single, ...

single(x) = changes the data type precision of x to single.

The default data type of numeric values in MATLAB is double which need 8 bytes of memory, single needs just 4 bytes.

realmin('DataType') = returns the minimum real value of the data type.

realmax('DataType') = returns the maximun real value of the data type.

For e.g. to get the minimum real value of the double precision data type and the maximum real value of the single precision data type.

>> realmin('double')

>> realmax('single')

Integer values also a valid data types in MATLAB in the two forms signed or unsigned as:

int8, 16, 32, 64

uint8, 16, 32, 64

 

Strings data types:

To define a string or entering it use:

>> x='string';

>> x='hello matlab';

>> x='let''s go with matlab';

MATLAB saves strings as a matrix or a vector of characters each character needs two bytes. this char's vector or matrix can be extracted to elements or combined with other elements as in numeric matrices.

num2str(x) = converts x to string of characters, where x is a numeric value. Then can be passed with characters or strings.e.g.

>> name = 'Almontaser';

>> nick = 'kabtin';

>> email = 'alkabtin999@outlook.com';

>> phoneNo = 0249918814188;

>> ['my name is : ' name ', my NickName is: ' nick ', my Email is: ' email ', my phone No. ' num2str(phoneNo(]

ans =
my name is : Almontaser, my NickName is: kabtin, my Email is: alkabtin999@outlook.com, my phone No. 249918814188

strcmpi('string1','string2') = compares the two strings; if they have different characters returns 0,  if not returns 1, capital and small letters doesn't different.

 

Structure data type:

Stores values for many variables as follow:

>> MY.name = 'Almontaser';


>> MY.nick = 'Kabtin';


>> MY.mail = 'alkabtin999@outlook.com';


>> MY.phone = '0249918814188';


>> address.contury = 'Sudan';


>> address.town = 'Khartoum';


>> address.htown = 'Omdurman';


>> address.home = sym(2/5);


>> address.homeNO = 122;


>> MY.address = address


>> MY

MY =

       name: 'Almontaser'

       nick: 'Kabtin'

       mail: 'alkabtin999@outlook.com'

      phone: '0249918814188'

    address: [1x1 struct]


>> MY.address

ans =

    contury: 'Sudan'

       town: 'Khartoum'

      htown: 'Omdurman'

       home: [1x1 sym]

     homeNO: 122


>> MY.address.home

ans = 2/5

 

Cell array data type:

It's like a matrix but it can include multiple types of data and it can includes a sub-cells and it use{ & } instead of [ & ], for e.g.

>> hometown = {'omdurman' sym(2/5) 'Hai alarab' 122};


>> local = {'SUDAN' 'KARTOUM' 123};


>> info = {local hometown}

info =

    {1x3 cell}    {1x4 cell}


>> info(1)

ans =     {1x3 cell}


>> info{1}

ans =     'SUDAN'    'KARTOUM'    [123]

 

Function types:


Functions can has many types depending on the method that function entered by it, if the function entered symbolically by function handle "@" or by "solve" commands then function type will be symbolic. And if function entered by "inline" command, then will be inline data type.

Note: function type can be found in TuTeR 4 & 5 which used in calculus and algebraic calculations.

MATLAB TuTeR 4: Algebraic calculations



MATLAB TuTeR 5: Calculus Calculation

 

Logical type:

The results of compassions or logic operators are always zero or one which is represents the Boolean result; true or false.

Boolean operator
 Means
==
Equal.
Smaller than.
Greater than.
<=
Smaller than or equal.
>=
Greater than or equal.

No comments:

Post a Comment