Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The...

81
1 © 2014 The MathWorks, Inc. What’s New in MATLAB Joe Hicklin

Transcript of Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The...

Page 1: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

1 © 2014 The MathWorks, Inc.

What’s New in MATLAB

Joe Hicklin

Page 2: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

2

My Favorite New Features in MATLAB

Tables

The MATLAB unit testing framework

Page 3: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

3

What is a table?

A new fundamental type in MATLAB

What are they for?

– You have several variables

– The N’th elements of each variable are related to one another

Page 4: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

4

What is a table?

Database tables

Page 5: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

5

What is a table?

Database tables

Excel tables

Page 6: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

6

What is a table?

Database tables

Excel tables

Files with Comma

Separated Values

(CSV)

Page 7: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

7

What is a table?

Database tables

Excel tables

CSV files

A very common data format.

– Each column is like a variable.

– They are grouped because they are related.

Page 8: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

8

A MATLAB Table

T =

LastName Gender Age Smoker Systolic Diastolic Status

__________ ________ ___ ______ ________ _________ ___________

'Smith' 'Male' 38 1 124 93 'Excellent'

'Johnson' 'Male' 43 0 109 77 'Fair'

'Williams' 'Female' 38 0 125 83 'Good'

'Jones' 'Female' 40 0 117 75 'Fair'

'Brown' 'Female' 49 0 122 80 'Good'

'Davis' 'Female' 46 0 121 70 'Good'

'Miller' 'Female' 33 1 130 88 'Good'

'Wilson' 'Male' 40 0 115 82 'Good'

'Moore' 'Male' 28 0 115 78 'Excellent'

'Taylor' 'Female' 31 0 118 86 'Excellent‘

>>

Page 9: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

9

A Table in the

Variable Editor

Page 10: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

10

What is a “fundamental type”?

We strive for both simplicity and power.

History of new fundamental types

– Array MATLAB 1.0

– Structures MATLAB 5.0

– Cell Arrays MATLAB 5.0

– Tables MATLAB R2013b

Page 11: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

11

Homogeneous --------------------- Heterogeneous

X Y Z

1 'A' [2,3;

4 'B' 5,6;

7 'C' 7,8]

Table

{ 1 2 'X';

4 'A' 6;

[1 2] 8 9 }

Cell Array

[ 1 2 3;

4 5 6;

7 8 9 ]

Array

Page 12: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

12

Irregular ------------------ Rectangular

X: [1 2 3 4 5]

Y: [1 2]

Z: 'lowPass'

Struct

X Y Z

1 'A' [2,3;

4 'B' 5,6;

7 'C' 7,8]

Table

Page 13: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

13

Making a Table

From workspace data

From an Excel file

From a file of Comma Separated Values (CSV)

Page 14: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

14

>> firstName = { 'Joe'; 'Mary'; 'Bob'; 'Susan' }

Page 15: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

15

>> firstName = { 'Joe'; 'Mary'; 'Bob'; 'Susan' }

firstName =

'Joe'

'Mary'

'Bob'

'Susan'

Page 16: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

16

>> Age = [ 34; 32; 54; 34 ]

Page 17: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

17

>> Age = [ 34; 32; 54; 34 ]

Age =

34

32

54

34

Page 18: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

18

>> Gender = { 'Male'; 'Female'; 'Male'; 'Female' }

Page 19: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

19

>> Gender = { 'Male'; 'Female'; 'Male'; 'Female' }

Gender =

'Male'

'Female'

'Male'

'Female'

Page 20: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

20

>> T = table( firstName, Gender, Age )

Page 21: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

21

>> T = table( firstName, Gender, Age )

T =

firstName Gender Age

_________ ________ ___

'Joe' 'Male' 34

'Mary' 'Female' 32

'Bob' 'Male' 54

'Susan' 'Female' 34

Page 22: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

22

Making a Table

From workspace data

From an Excel file

From a file of Comma Separated Values (CSV)

Page 23: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

23

% T = readtable('patients.xls')

T = readtable('patients.csv')

Page 24: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

24

% T = readtable('patients.xls')

T = readtable('patients.csv')

T =

LastName Gender Age Location Height Weight Smoker

____________ ________ ___ ___________________________ ______ ______ ______

'Smith' 'Male' 38 'County General Hospital' 71 176 1

'Johnson' 'Male' 43 'VA Hospital' 69 163 0

'Williams' 'Female' 38 'St. Mary's Medical Center' 64 131 0

'Jones' 'Female' 40 'VA Hospital' 67 133 0

'Brown' 'Female' 49 'County General Hospital' 64 119 0

'Davis' 'Female' 46 'St. Mary's Medical Center' 68 142 0

'Miller' 'Female' 33 'VA Hospital' 64 142 1

'Wilson' 'Male' 40 'VA Hospital' 68 180 0

'Moore' 'Male' 28 'St. Mary's Medical Center' 68 183 0

'Taylor' 'Female' 31 'County General Hospital' 66 132 0

'Anderson' 'Female' 45 'County General Hospital' 68 128 0

'Thomas' 'Female' 42 'St. Mary's Medical Center' 66 137 0

'Jackson' 'Male' 25 'VA Hospital' 71 174 0

'White' 'Male' 39 'VA Hospital' 72 202 1

'Harris' 'Female' 36 'St. Mary's Medical Center' 65 129 0

'Martin' 'Male' 48 'VA Hospital' 71 181 1

Page 25: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

25

Indexing

MATLAB has three kinds of indexing:

–A( )

–A.foo

–A{ }

Tables support all three!

Plus a new kind, “named indexing”

Page 26: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

26

>> T = T(1:10,:)

Page 27: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

27

>> T = T(1:10,:)

T =

LastName Gender Age Location Height Weight Smoker

__________ ________ ___ ___________________________ ______ ______ ______

'Smith' 'Male' 38 'County General Hospital' 71 176 1

'Johnson' 'Male' 43 'VA Hospital' 69 163 0

'Williams' 'Female' 38 'St. Mary's Medical Center' 64 131 0

'Jones' 'Female' 40 'VA Hospital' 67 133 0

'Brown' 'Female' 49 'County General Hospital' 64 119 0

'Davis' 'Female' 46 'St. Mary's Medical Center' 68 142 0

'Miller' 'Female' 33 'VA Hospital' 64 142 1

'Wilson' 'Male' 40 'VA Hospital' 68 180 0

'Moore' 'Male' 28 'St. Mary's Medical Center' 68 183 0

'Taylor' 'Female' 31 'County General Hospital' 66 132 0

Page 28: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

28

>> T.Age

Page 29: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

29

>> T.Age

ans =

38

43

38

40

49

46

33

40

28

31

Page 30: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

30

T =

LastName Gender Age Location Height Weight Smoker

__________ ________ ___ ___________________________ ______ ______ ______

'Smith' 'Male' 38 'County General Hospital' 71 176 1

'Johnson' 'Male' 43 'VA Hospital' 69 163 0

'Williams' 'Female' 38 'St. Mary's Medical Center' 64 131 0

'Jones' 'Female' 40 'VA Hospital' 67 133 0

'Brown' 'Female' 49 'County General Hospital' 64 119 0

'Davis' 'Female' 46 'St. Mary's Medical Center' 68 142 0

'Miller' 'Female' 33 'VA Hospital' 64 142 1

'Wilson' 'Male' 40 'VA Hospital' 68 180 0

'Moore' 'Male' 28 'St. Mary's Medical Center' 68 183 0

'Taylor' 'Female' 31 'County General Hospital' 66 132 0

Page 31: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

31

>> T.Location = []

Page 32: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

32

>> T.Location = []

T =

LastName Gender Age Height Weight Smoker

__________ ________ ___ ______ ______ ______

'Smith' 'Male' 38 71 176 1

'Johnson' 'Male' 43 69 163 0

'Williams' 'Female' 38 64 131 0

'Jones' 'Female' 40 67 133 0

'Brown' 'Female' 49 64 119 0

'Davis' 'Female' 46 68 142 0

'Miller' 'Female' 33 64 142 1

'Wilson' 'Male' 40 68 180 0

'Moore' 'Male' 28 68 183 0

'Taylor' 'Female' 31 66 132 0

Page 33: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

33

Body Mass Index = 703 * weight/(height^2)

Page 34: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

34

>> T.BMI = 703 * T.Weight ./ (T.Height .^ 2)

Page 35: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

35

>> T.BMI = 703 * T.Weight ./ (T.Height .^ 2)

T =

LastName Gender Age Height Weight Smoker BMI

__________ ________ ___ ______ ______ ______ ______

'Smith' 'Male' 38 71 176 1 24.544

'Johnson' 'Male' 43 69 163 0 24.068

'Williams' 'Female' 38 64 131 0 22.484

'Jones' 'Female' 40 67 133 0 20.828

'Brown' 'Female' 49 64 119 0 20.424

'Davis' 'Female' 46 68 142 0 21.589

'Miller' 'Female' 33 64 142 1 24.372

'Wilson' 'Male' 40 68 180 0 27.366

'Moore' 'Male' 28 68 183 0 27.822

'Taylor' 'Female' 31 66 132 0 21.303

Page 36: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

36

>> T{:,[4,5,7]}

Page 37: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

37

>> T{:,[4,5,7]}

ans =

71.0000 176.0000 24.5443

69.0000 163.0000 24.0683

64.0000 131.0000 22.4836

67.0000 133.0000 20.8285

64.0000 119.0000 20.4241

68.0000 142.0000 21.5887

64.0000 142.0000 24.3716

68.0000 180.0000 27.3659

68.0000 183.0000 27.8220

66.0000 132.0000 21.3030

Page 38: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

38

>> T{:,{‘Height’, ‘Width’, ‘BMI’} }

Page 39: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

39

>> T{:,{‘Height’, ‘Width’, ‘BMI’} }

ans =

71.0000 176.0000 24.5443

69.0000 163.0000 24.0683

64.0000 131.0000 22.4836

67.0000 133.0000 20.8285

64.0000 119.0000 20.4241

68.0000 142.0000 21.5887

64.0000 142.0000 24.3716

68.0000 180.0000 27.3659

68.0000 183.0000 27.8220

66.0000 132.0000 21.3030

Page 40: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

40

T =

LastName Gender Age Height Weight Smoker BMI

__________ ________ ___ ______ ______ ______ ______

'Smith' 'Male' 38 71 176 1 24.544

'Johnson' 'Male' 43 69 163 0 24.068

'Williams' 'Female' 38 64 131 0 22.484

'Jones' 'Female' 40 67 133 0 20.828

'Brown' 'Female' 49 64 119 0 20.424

'Davis' 'Female' 46 68 142 0 21.589

'Miller' 'Female' 33 64 142 1 24.372

'Wilson' 'Male' 40 68 180 0 27.366

'Moore' 'Male' 28 68 183 0 27.822

'Taylor' 'Female' 31 66 132 0 21.303

Page 41: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

41

>> T = T(:,{'Height','Weight','BMI','Smoker','Age','Gender'})

Page 42: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

42

>> T = T(:,{'Height','Weight','BMI','Smoker','Age','Gender'})

T =

Height Weight BMI Smoker Age Gender

______ ______ ______ ______ ___ ________

71 176 24.544 1 38 'Male'

69 163 24.068 0 43 'Male'

64 131 22.484 0 38 'Female'

67 133 20.828 0 40 'Female'

64 119 20.424 0 49 'Female'

68 142 21.589 0 46 'Female'

64 142 24.372 1 33 'Female'

68 180 27.366 0 40 'Male'

68 183 27.822 0 28 'Male'

66 132 21.303 0 31 'Female'

Page 43: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

43

>> T = T(:,{'Height','Weight','BMI','Smoker','Age','Gender'})

T =

Height Weight BMI Smoker Age Gender

______ ______ ______ ______ ___ ________

71 176 24.544 1 38 'Male'

69 163 24.068 0 43 'Male'

64 131 22.484 0 38 'Female'

67 133 20.828 0 40 'Female'

64 119 20.424 0 49 'Female'

68 142 21.589 0 46 'Female'

64 142 24.372 1 33 'Female'

68 180 27.366 0 40 'Male'

68 183 27.822 0 28 'Male'

66 132 21.303 0 31 'Female‘

>> T = T(:,[4, 5, 7, 6, 3, 2])

Page 44: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

44

Other Features

Sorting

Cat [ , ] [ ; ]

Join

Page 45: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

45

>> T.Age = sort(T.Age)

Page 46: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

46

>> T.Age = sort(T.Age)

Disaster!

Page 47: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

47

>> T = sortrows(T,{'Age','Height'})

Page 48: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

48

>> T = sortrows(T,{'Age','Height'})

T =

Height Weight BMI Smoker Age Gender

______ ______ ______ ______ ___ ________

68 183 27.822 0 28 'Male'

66 132 21.303 0 31 'Female'

64 142 24.372 1 33 'Female'

64 131 22.484 0 38 'Female'

71 176 24.544 1 38 'Male'

67 133 20.828 0 40 'Female'

68 180 27.366 0 40 'Male'

69 163 24.068 0 43 'Male'

68 142 21.589 0 46 'Female'

64 119 20.424 0 49 'Female'

Page 49: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

49

>> T1 = T(:,1:3)

>> T2 = T(:,4:end)

T1 =

Height Weight BMI

______ ______ ______

71 176 24.544

69 163 24.068

64 131 22.484

64 119 20.424

67 133 20.828

64 142 24.372

68 142 21.589

68 180 27.366

68 183 27.822

66 132 21.303

T2 =

Smoker Age Gender

______ ___ ________

1 28 'Male'

0 31 'Male'

0 33 'Female'

0 38 'Female'

0 38 'Female'

1 40 'Female'

0 40 'Female'

0 43 'Male'

0 46 'Male'

0 49 'Female'

Page 50: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

50

>> T = [T1,T2]

T =

Height Weight BMI Smoker Age Gender

______ ______ ______ ______ ___ ________

71 176 24.544 1 28 'Male'

69 163 24.068 0 31 'Male'

64 131 22.484 0 33 'Female'

64 119 20.424 0 38 'Female'

67 133 20.828 0 38 'Female'

64 142 24.372 1 40 'Female'

68 142 21.589 0 40 'Female'

68 180 27.366 0 43 'Male'

68 183 27.822 0 46 'Male'

66 132 21.303 0 49 'Female'

Page 51: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

51

>> T1 = T(1:5,:)

>> T2 = T(6:end,:)

T1 =

Height Weight BMI Smoker Age Gender

______ ______ ______ ______ ___ ________

71 176 24.544 1 28 'Male'

69 163 24.068 0 31 'Male'

64 131 22.484 0 33 'Female'

64 119 20.424 0 38 'Female'

67 133 20.828 0 38 'Female'

T2 =

Height Weight BMI Smoker Age Gender

______ ______ ______ ______ ___ ________

64 142 24.372 1 40 'Female'

68 142 21.589 0 40 'Female'

68 180 27.366 0 43 'Male'

68 183 27.822 0 46 'Male'

66 132 21.303 0 49 'Female'

Page 52: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

52

>> T = [T1;T2]

T =

Height Weight BMI Smoker Age Gender

______ ______ ______ ______ ___ ________

71 176 24.544 1 28 'Male'

69 163 24.068 0 31 'Male'

64 131 22.484 0 33 'Female'

64 119 20.424 0 38 'Female'

67 133 20.828 0 38 'Female'

64 142 24.372 1 40 'Female'

68 142 21.589 0 40 'Female'

68 180 27.366 0 43 'Male'

68 183 27.822 0 46 'Male'

66 132 21.303 0 49 'Female'

Page 53: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

53

Joining Two Tables

T1 =

SearchTerm IpAddress Time

________________________ ________________ ________

'iOS 7' '105.116.17.117' 2:51 AM

'lolcat' '81.13.36.71' 3:54 AM

'HTML' '123.124.21.125' 5:22 AM

'Manchester Uninted' '123.63.103.19' 6:37 AM

'cute Kitty' '81.13.36.71' 8:10 AM

'MATLAB' '105.116.17.117' 11:57 AM

'I Can Has Cheezburger?' '81.13.36.71' 2:02 PM

'BT Sport' '84.22.91.5' 3:43 PM

'Americas Cup' '36.6.13.106' 4:18 PM

'funny cat videos' '81.13.36.71' 5:01 PM

'Miley Cyrus' '105.116.17.117' 6:06 PM

'Gareth Bale' '24.63.58.83' 11:02 PM

Page 54: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

54

Joining Two Tables

T2 =

IpAddress Name

________________ ________________

'24.63.58.83' 'Samuel Clemens'

'36.6.13.106' 'Niels Bohr'

'81.13.36.71' 'Jos Martin'

'123.63.103.19' 'Mic Jagger'

'84.22.91.5' 'SpongeBob'

'105.116.17.117' 'Nicola Tesla'

'123.124.21.125' 'Ted Cruz'

Page 55: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

55

Joining Two Tables

T2 =

IpAddress Name

________________ ________________

'24.63.58.83' 'Samuel Clemens'

'36.6.13.106' 'Niels Bohr'

'81.13.36.71' 'Jos Martin'

'123.63.103.19' 'Mic Jagger'

'84.22.91.5' 'SpongeBob'

'105.116.17.117' 'Nicola Tesla'

'123.124.21.125' 'Ted Cruz'

T1 =

SearchTerm IpAddress Time

________________________ ________________ ________

'iOS 7' '105.116.17.117' 2:51 AM

'lolcat' '81.13.36.71' 3:54 AM

'HTML' '123.124.21.125' 5:22 AM

'Manchester Uninted' '123.63.103.19' 6:37 AM

'cute Kitty' '81.13.36.71' 8:10 AM

'MATLAB' '105.116.17.117' 11:57 AM

'I Can Has Cheezburger?' '81.13.36.71' 2:02 PM

'BT Sport' '84.22.91.5' 3:43 PM

'Americas Cup' '36.6.13.106' 4:18 PM

'funny cat videos' '81.13.36.71' 5:01 PM

'Miley Cyrus' '105.116.17.117' 6:06 PM

'Gareth Bale' '24.63.58.83' 11:02 PM

Page 56: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

56

>> T = join(T1,T2)

Page 57: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

57

>> T = join(T1,T2)

T =

SearchTerm IpAddress Time Name

________________________ ________________ ________ ________________

'iOS 7' '105.116.17.117' 2:51 AM 'Nicola Tesla'

'lolcat' '81.13.36.71' 3:54 AM 'Jos Martin'

'HTML' '123.124.21.125' 5:22 AM 'Ted Cruz'

'Manchester Uninted' '123.63.103.19' 6:37 AM 'Mic Jagger'

'cute Kitty' '81.13.36.71' 8:10 AM 'Jos Martin'

'MATLAB' '105.116.17.117' 11:57 AM 'Nicola Tesla'

'I Can Has Cheezburger?' '81.13.36.71' 2:02 PM 'Jos Martin'

'BT Sport' '84.22.91.5' 3:43 PM 'SpongeBob'

'Americas Cup' '36.6.13.106' 4:18 PM 'Niels Bohr'

'funny cat videos' '81.13.36.71' 5:01 PM 'Jos Martin'

'Miley Cyrus' '105.116.17.117' 6:06 PM 'Nicola Tesla'

'Gareth Bale' '24.63.58.83' 11:02 PM 'Samuel Clemens'

Page 58: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

58

>> sortrows(T,{'Name'})

Page 59: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

59

>> sortrows(T,{'Name'})

ans =

SearchTerm IpAddress Time Name

________________________ ________________ ________ ________________

'lolcat' '81.13.36.71' 3:54 AM 'Jos Martin'

'cute Kitty' '81.13.36.71' 8:10 AM 'Jos Martin'

'I Can Has Cheezburger?' '81.13.36.71' 2:02 PM 'Jos Martin'

'funny cat videos' '81.13.36.71' 5:01 PM 'Jos Martin'

'Manchester Uninted' '123.63.103.19' 6:37 AM 'Mic Jagger'

'iOS 7' '105.116.17.117' 2:51 AM 'Nicola Tesla'

'MATLAB' '105.116.17.117' 11:57 AM 'Nicola Tesla'

'Miley Cyrus' '105.116.17.117' 6:06 PM 'Nicola Tesla'

'Americas Cup' '36.6.13.106' 4:18 PM 'Niels Bohr'

'Gareth Bale' '24.63.58.83' 11:02 PM 'Samuel Clemens'

'BT Sport' '84.22.91.5' 3:43 PM 'SpongeBob'

'HTML' '123.124.21.125' 5:22 AM 'Ted Cruz'

Page 60: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

60

>> sortrows(T,{'Name'})

ans =

SearchTerm IpAddress Time Name

________________________ ________________ ________ ________________

'lolcat' '81.13.36.71' 3:54 AM 'Jos Martin'

'cute Kitty' '81.13.36.71' 8:10 AM 'Jos Martin'

'I Can Has Cheezburger?' '81.13.36.71' 2:02 PM 'Jos Martin'

'funny cat videos' '81.13.36.71' 5:01 PM 'Jos Martin'

'Manchester Uninted' '123.63.103.19' 6:37 AM 'Mic Jagger'

'iOS 7' '105.116.17.117' 2:51 AM 'Nicola Tesla'

'MATLAB' '105.116.17.117' 11:57 AM 'Nicola Tesla'

'Miley Cyrus' '105.116.17.117' 6:06 PM 'Nicola Tesla'

'Americas Cup' '36.6.13.106' 4:18 PM 'Niels Bohr'

'Gareth Bale' '24.63.58.83' 11:02 PM 'Samuel Clemens'

'BT Sport' '84.22.91.5' 3:43 PM 'SpongeBob'

'HTML' '123.124.21.125' 5:22 AM 'Ted Cruz'

Page 61: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

61

Try out tables!

Tables are a new kind of variable

Use them when you have arrays of “related” data

Many more features that I haven’t shown:

– stack and unstack

– summary

– ismissing and standardizeMissing

– varfun and rowfun

– VariableUnits, Description, VariableDescriptions, …

Page 62: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

62

We Love Testing

Quality

Confidence

Productivity

Page 63: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

63

MATLAB Unit Test Framework

Based on x-Unit (SUnit, JUnit, CppUnit …)

Parts

– Test case

– Test suite

– Test runner

Page 64: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

64

function tests = TestTableIndexing

tests = functiontests(localfunctions);

end

function testDotIndex (testCase)

T = readtable('patients.csv');

byDot = T.Age;

byIndex = T{:,3};

verifyEqual(testCase,byDot,byIndex);

end

function testNameIndex (testCase)

T = readtable('patients.csv');

byName = T{:,'Age'};

byIndex = T{:,3};

verifyEqual(testCase,byName,byIndex);

end

Page 65: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

65

function tests = TestTableIndexing

tests = functiontests(localfunctions);

end

function testDotIndex (testCase)

T = readtable('patients.csv');

byDot = T.Age;

byIndex = T{:,3};

verifyEqual(testCase,byDot,byIndex);

end

function testNameIndex (testCase)

T = readtable('patients.csv');

byName = T{:,'Age'};

byIndex = T{:,3};

verifyEqual(testCase,byName,byIndex);

end

Page 66: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

66

function tests = TestTableIndexing

tests = functiontests(localfunctions);

end

function testDotIndex (testCase)

T = readtable('patients.csv');

byDot = T.Age;

byIndex = T{:,3};

verifyEqual(testCase,byDot,byIndex);

end

function testNameIndex (testCase)

T = readtable('patients.csv');

byName = T{:,'Age'};

byIndex = T{:,3};

verifyEqual(testCase,byName,byIndex);

end

Page 67: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

67

function tests = TestTableIndexing

tests = functiontests(localfunctions);

end

function testDotIndex (testCase)

T = readtable('patients.csv');

byDot = T.Age;

byIndex = T{:,3};

verifyEqual(testCase,byDot,byIndex);

end

function testNameIndex (testCase)

T = readtable('patients.csv');

byName = T{:,'Age'};

byIndex = T{:,3};

verifyEqual(testCase,byName,byIndex);

end

Page 68: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

68

function tests = TestTableIndexing

tests = functiontests(localfunctions);

end

function testDotIndex (testCase)

T = readtable('patients.csv');

byDot = T.Age;

byIndex = T{:,3};

verifyEqual(testCase,byDot,byIndex);

end

function testNameIndex (testCase)

T = readtable('patients.csv');

byName = T{:,'Age'};

byIndex = T{:,3};

verifyEqual(testCase,byName,byIndex);

end

Page 69: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

69

>> runtests TestTableIndexing.m

Page 70: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

70

>> runtests TestTableIndexing.m

Running TestTableIndexing

..

Page 71: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

71

>> runtests TestTableIndexing.m

Running TestTableIndexing

..

Done TestTableIndexing

__________

ans =

1x2 TestResult array with properties:

Name

Passed

Failed

Incomplete

Duration

Totals:

2 Passed, 0 Failed, 0 Incomplete.

0.089878 seconds testing time.

Page 72: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

72

Why Testing Might Be For You

Testing saves development time.

Testing makes development more enjoyable.

– Your time is spent making things, not fixing things.

– You are a designer, not a detective.

– Fewer nasty surprises and opportunities to look stupid

The framework is not trivial, but easily learnable.

– Well worth the effort if you maintain software.

Page 73: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

73

I’ve talked about…

Tables: a new fundamental type in MATLAB

The MATLAB unit testing framework

Page 74: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

74 © 2014 The MathWorks, Inc.

Page 75: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

© 2014 The MathWorks, Inc.

Mixed-Integer Linear Programming in

MATLAB

Seth DeLand

Page 76: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

Why are integer variables important?

Need a solver that finds

integer-feasible solutions

Page 77: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

Mixed-integer linear programming

Page 78: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

Mixed-integer linear programming

Continuous and integer variables

𝑥1 ∈ 0, 100 𝑥2 ∈ {1,2,3,4,5}

Linear objective and constraints

min𝑥

−𝑥1 − 2𝑥2

𝑥1 + 4𝑥2 ≤ 20 𝑥1 + 𝑥2 = 10

such that

Page 79: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

Traveling Salesman Problem

Problem

How to find the shortest path through a series of points?

Solution

Calculate distances between all combinations of points

Solve an optimization problem where variables correspond to trips

between two points

1

1 1

0

1

1

0

0

0

0

Page 80: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

For more information

Mixed-integer linear programming

www.mathworks.com/products/optimization/

>> doc optim

Web maps

www.mathworks.com/products/mapping/

>> webmap

Page 81: Tables The MATLAB unit testing framework€¦ · 2 My Favorite New Features in MATLAB Tables The MATLAB unit testing framework

© 2014 The MathWorks, Inc.

Questions?