34
© 2009 The MathWorks, Inc. ® ® Scuola di Calcolo Scientifico con MATLAB Palermo & Catania, Jan 27-29

Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

© 2

009

The

Mat

hWor

ks, I

nc.

® ®

Scuola di Calcolo Scientifico con MATLAB

Palermo & Catania, Jan 27-29

Page 2: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

2

® ®

Agenda

� Visualizing data

� How to build GUIs using MATLAB and GUIDE

� The GUI M-file(s)

� Working with user interface controls

� Callback programming and various advanced topics

� Development/Deployment Process Overview

� Deploying with MATLAB

Page 3: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

3

® ®

Conveying Information

1970 1975 1980 1985 1990 1995 2000 2005 20100

2

4

6

8

10

12

14

1970 1975 1980 1985 1990 1995 2000 20050

5

10

15

20

25

year

num

ber

of h

urric

anes

TS1

2

3

45

longitude

latit

ude

Hurricane paths for 2004

-100 -80 -60 -40 -20 00

10

20

30

40

50

60

-100 -80 -60 -40 -20 0 200

10

20

30

40

50

60

70

800

1000

1200

1400

1600

S&

P 5

00

S&P500 high

01/04 01/05 01/06 01/07 01/08 01/092500

3000

3500

4000

4500

date

NZ

SE

50

7.32 7.322 7.324 7.326 7.328 7.33 7.332 7.334 7.336 7.338

x 105

2500

3000

3500

4000

4500

7.1 7.15 7.2 7.25 7.3 7.35

x 105

0

500

1000

1500

2000

Page 4: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

4

® ®

1%2%< 1%< 1%2%< 1%< 1%< 1%2%< 1%< 1%1%< 1%< 1%

2%< 1%< 1%< 1%

1%< 1%1%

< 1%< 1%< 1%< 1%

2%< 1%

1%< 1%< 1%

2%1%

2%2%

1%3%

1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%

2%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%

< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%< 1%

1 2 3 4 5 6

5

10

15

20

25

30

35

0 2 40 2 40 2 40 2 40 5 100 10 200

2

4024

0

2

40

2

40

5

100

10

20

1970 1975 1980 1985 1990 1995 2000 2005 20100

2

4

6

8

10

12

14

1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6

5

10

15

20

25

30

35

1970 1975 1980 1985 1990 1995 2000 20050

5

10

15

20

25

30

0

2

4

6

8 010

2030

40

0

2

4

6

8

10

12

14

1970 1975 1980 1985 1990 1995 2000 20050

5

10

15

20

25

year

num

ber

of h

urric

anes

TS1

2

3

45

Plot Catalog

Page 5: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

5

® ®

Estimate Electricity Usage

plot contourf ribbon

Filename: january.txt

Page 6: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

6

® ®

-0.5 0 0.5 1 1.5 2 2.5-1

-0.5

0

0.5

1

1.5

horizontal displacement (x) [cubits]

��������

☯��

� � �

This graph is seriously annotated!3 ≤ ω < π

� � ���

����������

local maximum

global maximum

penguins

sloths(as t → ∞)

Annotation

ylabel

xlabel

textgtext

title

legend

grid

Page 7: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

7

® ®

Customizing Axes

Page 8: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

8

® ®

Handle Graphics ®

figure

axes

plot objects

(text/line/patch/etc)

Page 9: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

9

® ®

Property Inspector and Browser

Page 10: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

10

® ®

Get and Set

>> [c,ch] = contour(data,10);>> set(ch,'LineWidth',1.5)

Page 11: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

11

® ®

Navigating the Hierarchy

>> h1 = get(gca,'Parent')>> h2 = get(gca,'Children')>> set(h2,'LineStyle', ':‘)

Page 12: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

12

® ®

ExerciseBarc(y,label) draws the column of the 1-by-M y in different colors and labels the axes with custom te xt.

label is a 1-by-M cell array of strings.

label = …{'car','trucks','freight','barges','rail',planes'};

Y = [8 9 10 7 6 5]

Page 13: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

13

® ®

guide

Page 14: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

14

® ®

sommatore

edit

text

Page 15: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

15

® ®

Page 16: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

16

® ®

Interface Challenges

� Who are your users?� Predictability� Feedback and error control� Undo capabilities� Disable irrelevant options

Page 17: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

17

® ®

Design Principles

Consistency Alignment Integrity

Harmony

Simplicity Unity Clarity

Elegance Directness

Familiarity Friendliness

Comfort Charm

Page 18: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

18

® ®

Dynamic Interfaces

ContinuousPhysical

Smooth

ImmediateSnappy

Direct

Page 19: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

19

® ®

Design Process

Page 20: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

20

® ®

Resources

� http://www.math.duke.edu/education/ccp/resources/write/design

� http://www.fgcu.edu/onlinedesign/index.html

� http://cfg.cit.cornell.edu/design/contents.html

Page 21: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

21

® ®

Earth-Quake Data Analysis

� Scenario:– Develop a tool to analyze earthquake

data

� Requirements:– Statistics capabilities

– Signal Processing Algorithms

– Automated program

– User interface

– Reporting capability

Page 22: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

22

® ®

Data Analysis with MATLAB

Earth-Quake Data Analysis

quake.csv

Page 23: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

23

® ®

Earth-Quake Analysis

Step1: Import and Visualize Data

� Import data from quake.xls file

� Scale the data

� Plot data� Customize and annotate data

� Save your work

Page 24: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

24

® ®

Earth-Quake Analysis

Step1: Import Data

1. Move to your working directory using the Current Directory Browser.

2. Use the Import Wizard to load the data in quake.xls into a 10001 x 3 array data in the MATLAB Workspace.

3. In the Workspace Browser, select data . Use the Plot Selection menu at the top of the Workspace Browser to plot data .

4. Scale data by the gravitational acceleration g = 9.8 m/s^2. Please consider that data unit is cm/s^2.

5. Create a variable t for the time history.

6. In the Workspace Browser, select data and t . Use the Plot Selection menu at the top of the Workspace Browser to plot them

7. Enable Plot Tools with button on Figure Toolbar. Enhance the plot by adding titles and axis labels, changing fonts and colors, adjusting axes and plot properties.

8. Save your work to a MATLAB figure (quake.fig ) file in your working directory and save your variables to a mat-file (step1.mat )

Page 25: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

25

® ®

Earth-Quake Analysis

Step2: Prepare Data

� Remove/replace missing values

� Smooth and detrend data

� Visualize original and processed data

� Write custom script to automate workflow

� Interactively explore data

Page 26: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

26

® ®

Earth-Quake Analysis

Step2: Prepare Data

1. Open data in the Array Editor. Create a 10001 x 1 array e from the 1st column of data , and a 10001 x 1 array n from the 2nd one.

2. In the Workspace Browser, select e and t . Use the Plot Selection menu at the top of the Workspace Browser to plot e.

3. Enable Plot Tools with button on Figure Toolbar. Click 2D Axes in the New Subplot panel of the Figure Palette. Once the axes appears, the Add Data button on the Plot Browser is activated. Click this button to visualize n.

4. Use the Data Statistics Tool to find the mean acceleration in the East-West and North-South directions

5. Interactively zoom to explore e in the range [21 22.5]s

6. Use the MATLAB function isnan to identify missing values in e, and then remove them: � Replace missing values from “long term average” value� Interpolate missing values from neighboring value

t

Page 27: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

27

® ®

Earth-Quake Analysis

Step2: Prepare Data

7. Search a MATLAB function for removing linear trends. Hint: docsearch('removing linear trend')

8. Write a custom script to automate workflow:– Create M-file from selected commands in Command History– Use a for loop to identify and interpolate missing values in data– Save the M-file as preProcess.m

Page 28: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

28

® ®

Earth-Quake Analysis

Step3: Analyze Data

� Sharing Results from MATLAB– Publish m-file scripts– Export data and graphics

� Analyze the frequency content of your data

� Denoise/filter data

� Interactively locate the earthquake

Page 29: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

29

® ®

Earth-Quake Analysis

Step3: Analyze Data

1. Using the fft command perform the Fast Fourier Transform of detrend_data and evaluate its power spectrum Pyy .

2. Use filter for denoising your signal. Apply a band-pass filter to eliminate frequency above and below the range [2-14]Hz.

3. Use cumsumto integrate filteredData and return the approximate x, y, and z coordinates of the seismometer during the earthquake. Write a custom function seisTraj.m to automate the integration.

4. Use print to save the 3-D plot (plot3 ) of the seismometer’s trajectory to a bmp file (e.g. sies.bmp ).

5. Visualize filteredData in three different subplots. Use ginput to measure the S-P time interval. The S-P time interval will be used to determine the distance the waves have traveled from the origin to the station.

Page 30: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

30

® ®

Earth-Quake Analysis

Step4: Building an Application

� Convert script to reusable function– Code quality– Performance

� Add a graphical user interface– Use pre-defined dialog boxes

(select files, print graphics, …)– Develop a complete custom graphical user interface

Page 31: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

31

® ®

Earth-Quake Analysis

Step4: Building an Application

1. Converting the following script m-Files to function m-Files – preProcess.m– preFilter.m– seistraj.m

2. At the MATLAB prompt run guide('seis.fig') . Look for a

subfunction that is named PSMenu_Callback and perform all

TODOstatements. Test if your application works.

4. Compile your application with the following command: deploytool

Page 32: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

32

® ®

MATLABBuilder for .NET

MATLABBuilder for Excel

MATLABBuilder for

Java

.exe .dll/.lib .NETCOMExcelAdd-in

MATLAB Compiler

Java

Deploying Applications with MATLAB

www

Page 33: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

33

® ®

MATLABMATLAB

DesktopDesktop

EndEnd--UserUser

MachineMachine

Working with MATLAB Compiler

1

2

3

Page 34: Scuola di Calcolo Scientifico con MATLAB · 2 ® ® Agenda Visualizing data How to build GUIs using MATLAB and GUIDE The GUI M-file(s) Working with user interface controls Callback

© 2

009

The

Mat

hWor

ks, I

nc.

® ®

Questions?