7. Data Types

 

7.1   Identifying strings, numbers of different types, and logical values (Booleans)

7.2   Converting characters to number and vice versa

7.3   Creating and accessing cells

7.4   Creating and accessing structures

7.5   Practicing data types

 

 

top of page

7.1 Identifying strings, numbers of different types, and Booleans

 

 

Code 7.1.1:

 

clear all

 

a = 'a'

b = 1

c = 1.00

d = round(c)

e = single(d)

f = true

g = false

 

whos

 

Output  7.1.1:

 

a =

a

 

b =

     1

 

c =

     1

 

d =

     1

 

e =

     1

 

f =

     1

 

g =

     0

 

  Name      Size                    Bytes  Class

 

  a         1x1                         2  char array

  b         1x1                         8  double array

  c         1x1                         8  double array

  d         1x1                         8  double array

  e         1x1                         4  single array

  f         1x1                         1  logical array

  g         1x1                         1  logical array

 

Grand total is 7 elements using 32 bytes

 

Code 7.1.2:

 

help datatypes

 

Output  7.1.2:

 

  Data types and structures.

 

  Data types (classes)

    double          - Convert to double precision.

    logical         - Convert numeric values to logical.

    cell            - Create cell array.

    struct          - Create or convert to structure array.

    single          - Convert to single precision.

    uint8           - Convert to unsigned 8-bit integer.

    uint16          - Convert to unsigned 16-bit integer.

    uint32          - Convert to unsigned 32-bit integer.

    uint64          - Convert to unsigned 64-bit integer.

    int8            - Convert to signed 8-bit integer.

    int16           - Convert to signed 16-bit integer.

    int32           - Convert to signed 32-bit integer.

    int64           - Convert to signed 64-bit integer.   

 

Code 7.1.3:

 

double_value = 2

 

class(double_value)

 

single_value = single(double_value)

 

class(single_value)

 

Output  7.1.3:

 

double_value =

 

     2

 

ans =

 

double

 

single_value =

 

     2

 

ans =

 

single

 

 

top of page

7.2 Converting characters to numbers and vice versa

 

Code 7.2.1

 

de = double('!')

 

dq = double('Our days are numbered!')

 

Output 7.2.1

 

de =

 

    33

 

 

dq =

 

  Columns 1 through 14

 

    79   117   114    32   100    97   121   115    32    97   114   101    32   110

 

  Columns 15 through 22

 

   117   109    98   101   114   101   100    33

 

Code 7.2.2

 

de_lettered = char(de)

 

dq_lettered = char(dq)

 

Output 7.2.2

 

de_lettered =

 

!

 

dq_lettered =

 

Our days are numbered!

 

top of page

7.3 Creating and accessing cells

 

Code 7.3.1

 

clear all

c = [1:2;3:5]

 

Output 7.3.1

 

??? Error using ==> vertcat

All rows in the bracketed expression must have the same

number of columns.

 

Code 7.3.2

 

clear c

c = {1:2;3:5}

 

Output 7.3.2

 

c =

 

    [1x2 double]

    [1x3 double]

 

 

Code 7.3.3

 

c1 = cell2mat(c(1))

c2 = cell2mat(c(2))

c2(2:3)

 

Output 7.3.3

 

c1 =

1 2

 

c2 =

3 4 5

 

ans =

4 5

 

Code 7.3.4

 

c = {[ 1 2 3]

     [4 5 6 7]

     ['rats mice']; [' d']

     [['rats mice'] [' d']]

     [1 3]}

 

c_first_row = c{1,:,:}

c_second_row = c{2,:,:}

c_third_row = c{3,:,:}

c_third_row_again = c{3}

 

disp('c first row column 1 and second row column 1')

% Assigning this to a variable would cause problems

c{1:2,1}

 

d = {[1 2] [3 4 5]

     [6 7] [8 9 10]}

 

d_first_row_first_column = d{1,1}

d_first_row_first_column_element_1 = d{1,1}(2)

d_second_row_second_column_elements_2_and3 = d{2,2}(2:3)

 

Output 7.3.4

 

c =

 

    [1x3 double]

    [1x4 double]

    'rats mice'

    ' d'

    'rats mice d'

    [1x2 double]

 

c_first_row =

 

     1     2     3

 

c_second_row =

 

     4     5     6     7

 

c_third_row =

 

rats mice

 

c_third_row_again =

 

rats mice

 

c first row column 1 and second row column 1

 

ans =

 

     1     2     3

 

ans =

 

     4     5     6     7

 

d =

 

    [1x2 double]    [1x3 double]

    [1x2 double]    [1x3 double]

 

 

d_first_row_first_column =

 

     1     2

 

d_first_row_first_column_element_1 =

 

     2

 

d_second_row_second_column_elements_2_and3 =

 

     9    10

 

Code 7.3.5

 

Names_and_Numbers = {

'Bob' 90

'Jane' 100

}

cell2mat(Names_and_Numbers(1,1))

cell2mat(Names_and_Numbers(1,2))

 

Output 7.3.5

 

Names_and_Numbers =

'Bob' [ 90]

'Jane' [100]

ans =

Bob

ans =

90

 

 

top of page

7.4 Creating and accessing structures

 

 

Code 7.4.1

 

house(1).kitchen = 30^2;

house(1).dining_room =  40^2;

house(1).living_room = 50^2;

 

house(2).kitchen = 40^2;

house(2).dining_room =  50^2;

house(2).living_room = 60^2;

 

house(3).kitchen = 50^2;

house(3).dining_room =  60^2;

house(3).living_room = 70^2;

 

house

house(3)

house(2:3).kitchen

house(:).living_room

 

totalkitchen = 0;

for k=1:3

    totalkitchen = house(k).kitchen + totalkitchen;

end

totalkitchen

 

Output 7.4.1

 

house =

1x3 struct array with fields:

    kitchen

    dining_room

    living_room

 

ans =

        kitchen: 2500

    dining_room: 3600

    living_room: 4900

 

ans =

        1600

 

ans =

        2500

 

ans =

        2500

 

ans =

        3600

 

ans =

        4900

 

ans =

        5000

 

 

 

Code 7.4.2

 

subject(1).RTs = [

    500 400 350;

    450 375 325

    ];

subject(1).errors = [

    10 8 6;

    4 3 2

    ] ;

 

subject(2).RTs = [

    600 500 450;

    550 475 425;

    500 425 400

    ];

 

subject(2).errors = [

    10 8 6;

    4 3 2

    3 2 1

    ] ;

 

subject(2).debrief = 1

 

subject(2).comment = 'That was a really cool experiment!';

 

subject

 

for s = 1:2

    subject(s).RTs(:,:)

    subject(s).errors(:,:)

    subject(s).debrief(:,:)

end

 

Output 7.4.2

 

subject =

 

1x2 struct array with fields:

    RTs

    errors

    debrief

 

ans =

 

   500   400   350

   450   375   325

 

ans =

 

    10     8     6

     4     3     2

 

ans =

 

     []

 

ans =

 

   600   500   450

   550   475   425

   500   425   400

 

ans =

 

    10     8     6

     4     3     2

     3     2     1

 

ans =

 

     1

 

ans =

 

That was a really cool experiment!