Top Banner
753

MATLAB 7. Программирование, численные методы

Jul 28, 2015

Download

Documents

pacicus
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

M

A

T

L

A

B

7

,

- - 2005

681.3.06 32.973.26-018.2 37 . . , . ., . . MATLAB 7: , . .: -, 2005. 752 : . ISBN 5-94157-347-2 MATLAB. , , , , . ,

37

681.3.06 32.973.26-018.2

: . . .

Na 02429 24.07.00. 22.03.05. 70x1001/ie. . . . . 60,63. 3000 . 899 "-", 194354, -, . , 5. - 77.99.02.953..006421.11.04 11.11.2004 . . " "" 199034, -, 9 , 12

ISBN 5-94157-347-2

. ., . . 2005 , "-", 2005

1. 1.1. 1.2. MATLAB 7 2. 2.1. MATLAB 7 2.2. 2.3. ? 2.4. 2.5. 2.5.1. 2.5.2. 2.5.3. , 2.5.4. 2.5.5. 2.6. 2.6.1. 2.6.2. 2.6.3. 2.6.4. 2.6.5. 2.6.6. Edit View Insert Tools 2.6.7. 2.7. 2.7.1. 2.7.2. 2.7.3. 2.7.4.

1 3 3 5 13 13 20 22 25 29 29 30 33 37 39 43 43 50 52 52 55 60 60 67 69 69 76 77 77 81 83 86

IV 3.

89 89 90 92 98 100 101 104 105 106 107 108 113 119 123 123 127 127 133 134 135 137 144 146 148 149 152 155 160 165 165 171 174 178 183 183 192 198 207 : 209 212

3.1. MATLAB 3.1.1. (char arrays) 3.1.2. MATLAB 3.1.3. (single arrays) 3.1.4. (sparse arrays) 3.1.5. (struct arrays) 3.1.6. (cell arrays) 3.1.7. (function handle arrays) 3.1.8. (logical arrays) 3.1.9. 3.2. 3.3. 3.4. 4. 4.1. 4.2. 4.3. MATLAB 4.4. 4.5. 4.6. 4.7. 4.8. eval vifeval 4.9. m- 4.9.1. Text 4.9.2. Debug 4.9.3. Edit 4.10. 4.11. 5. 5.1. 5.2. 5.3. 5.4. 5.5. 5.6. 5.7. 5.8. (sprint/, sscanf)

6. 6.1. 6.2.

6.3. 6.4. 6.4.1. 6.4.2. 6.5. 6.5.1. 6.5.2. 6.5.3. 6.5.4. 6.6. 6.7. 6.8. 6.9. 7. 7.1. 7.2. 7.3. 7.4. 7.5. 7.6. 7.7. 7.8. Root Figure Axes Line Rectangle Text Image 212 213 214 220 223 224 224 225 234 238 238 239 250 255 260 266 274 283 287 291 298 312 315 321 328 335 336 341 345 347 350 352 353 360 365 371 372 372 380 387

8. 8.1. 8.1.1. 8.1.2. , 8.1.3. , 8.1.4. , 8.1.5. 8.1.6. 8.2. 8.3. GUIDE 8.3.1. GUIDE .' 8.3.2. 8.3.3. 8.3.4. (Property Inspector) 8.3.5. (Object Browser) 8.3.6. (Menu Editor) 8.3.7. , GUIDE 8.3.8.

Vl_ 9. 9.1. (dialog) 9.2. (errordlg) 9.3. (helpdlg) 9.4. (inputdlg) 9.5. (listdlg) 9.6. (tnsgbox) 9.7. (pagedlg) 9.8. (pagesetupdlg) 9.9. (printdlg) 9.10. (questdlg) 9.11. (uigetfile) 9.12. (uiputfile) 9.13. (uisetcolor) 9.14. (uisetfont) 9.15. (warndlg) 9.16. 10. 10.1. 10.1.1. 10.1.2. 10.1.3. 10.1.4. 10.1.5. 10.2. 10.2.1. Surface 10.2.2. 10.2.3. 10.2.4. : 10.2.5. 10.2.6. 10.2.7. 10.3. 10.3.1. Patch 10.3.2. Patch 10.4. . 10.4.1. 10.4.2. 11. 11.1. 11.2. ,

393 394 395 396 397 399 402 403 405 406 408 409 412 413 415 416 417 421 421 421 423 424 424 425 427 427 432 436 437 438 440 442 448 448 455 460 460 462 465 465 466

11.2.1. 11.2.2. , 11.2.3. 11.2.4. 11.2.5. 11.3. , 11.3.1. 11.3.2. 11.3.3. 11.3.4. 11.3.5. 12. 12.1. 12.2. 12.3. , 12.4. 12.5. 12.5.1. 12.5.2. , 12.5.3. () 12.6. 12.6.1. 12.6.2. 12.6.3. 12.6.4. 12.6.5. 12.6.6. 12.6.7. 12.7. 12.7.1. 12.7.2. 12.7.3. LU- 12.7.4. 12.7.5. QR- 12.7.6. 12.7.7. 12.7.8. 12.7.9. 12.7.10. 12.8. 12.8.1.

Vll_ 466 467 468 469 470 471 471 472 473 474 476 479 479 481 487 488 490 490 490 490 490 490 491 491 492 492 493 494 494 494 496 498 500 500 503 504 505 507 509 511 511 511 511

VIII 12.8.2. 12.8.3. 12.8.4. 12.9. 12.9.1. 12.9.2. 12.9.3. 12.10. 13. 13.1. 13.2. 13.2.1. MATLAB 13.2.2. 13.2.3. 13.3. 13.4. 13.4.1. 13.4.2. 14. 14.1. 14.2. 14.3. 14.4. 14.5. 14.6. 14.7.

512 515 517 517 519 522 522 523 524 525 526 527 529 535 535 539 543 544 546 549 553 553 555 559 559 560 562 563 572 580 588 595 595 599 611 614 614 618 624 629

15. 15.1. 15.2. 15.3. 15.4. 15.4.1. fminsearch 15.4.2. /minunc 15.4.3. hqnonlin 15.4.4. fminimax

15.5. 15.5.1. fmincon 15.5.2. hqnonlin wfminimax 15.6. 16. 16.1. 16.2. 16.3. 16.4.

IX_ 637 638 646 649 653 653 658 662 667 675 676 677 678 683 686 695 699

17. - 17.1. 17.2. 17.3. 17.4. 17.5. 17.6. 17.7.

705

1. MATLAB Help- 2. - MATLAB

707 708 710 713 715 723 723 725 727 728

MATLAB . MATLAB Basic, C++, Java, Object Pascal. MATLAB ( , ). , . , MATLAB . MATLAB , . MATLAB . - MATLAB , , . , MATLAB, , . MATLAB , , . . . , MATLAB . MATLAB. , , , , -

. . . , . . , . . , . . , . . , . . , . . . . . - , . .

1

MATLAB 7 MathWorks, 2004 . , , .

1.1. MATLAB . (Cleve . Mouler). MathWorks - (-, , ). , ([66, 67]). 30 Fortran (LINPACK) (EISPACK). 1980 . AFIPS "Design of an interactive matrix calculator", , , MATLAB. " " ( 6200 1980 .) : " ( ) MATLAB, . MATLAB , . , FOR WHILE, , . 37 MATLAB EISPACK (70 )

_4

1

UNPACK (40 ) . Fortran , ". MATLAB (Jack Little) MathWorks, 80- MATLAB VAX, Macintosh IBM PC. MathWorks, - . , . (1984 .) . . MATLAB 1000 MathWorks. 2004 . MATLAB : 1. MATLAB 5 - 1996 . 2. MATLAB 5.1 - 1997 . 3. MATLAB 5.3 (Release 11, Rll) - 1999 . 4. MATLAB 6.0 (R12) - 2000 . 5. MATLAB 6.1 (R12.1) - 2001 . 6. MATLAB 6.5 (R13) - 2002 . 7. MATLAB 6.51 (R13SP1) 2003 . 8. MATLAB 7.0 (R14) 2004 . MATLAB , . . www.softline.ru, www.matlab.ru www.exponenta.ru.

, MATLAB, MATLAB Softline. MATLAB 600 , , . -

15 (. -).

MATLAB . , . , .

1.2. MATLAB 7 MATLAB 7: 12 , MATLAB ; 28 ; ( , , Simulink) ; ; MATLAB ; ; . "MATLAB 7.0 Release Notes" 274 .

MATLAB 7 - : Pentium III, Pentium 4, Pentium M, Xeon, AMD Athlon, Athlon XP, Athlon MP; Microsoft Windows XP, Windows NT 4.0 (Service Pack 5 6a) , Windows 2000 (Service Pack 3 4); CD-ROM ; 256 ( 512 ); PDF. ,

6

1

. MATLAB 1 ; 256 , (16, 24 32 ), OpenGL. : , , ; Microsoft Word (Office 2000) Office XP MATLAB Notebook; TCP/IP . mex- : Compaq Visual Fortran ( 5.0, 6.1 6.5); Microsoft Visual C/C++ ( 5.0, 6.0 7.0); Borland C/C++ ( 5.0 5.02); Borland C++Builder ( 3.0, 4.0, 5.0 6.0); WATCOM ( 10.6 11); Lcc 2.4 (bundled with MATLAB). / online- Netscape Navigator ( 4.0 ) Microsoft Internet Explorer ( 4.0 ), Adobe Acrobat Reader ( 3.0 ). MATLAB , . () , , (). , , . (, , , , ), , . . 5060- -5 -9, -8, -10 ,

-8, 6080 . . , . 56 3040. , ( ), . . . - ( ""). , , . Simulink, MATLAB ( ), . , , , . [1, 2, 7, 8, 12, 14, 19, 20, 39]. MATLAB. , , Blockset, ( mdl), . MATLAB Toolbox ( ). , , . m- MATLAB. MATLAB 7 . 1.1. , . "", . , MATLAB , , . * , . **.

8

1 1.1 / 1* 2 3** 4 MATLAB Simulink, ver6.0 Aerospace Blockset, ver 1.6 Bioinformatics Toolbox, ver 1.1 CDMA Reference Blockset, ver 1.1 Communications Blockset, ver 3.0 Communications Toolbox, ver 3.0 Control System Toolbox, ver 6.0 Curve Fitting Toolbox, ver 1.1.1 Data Acquisition Toolbox, ver 2.5 , ( Simulink) IS-95A (code division multiple access, CDMA) Communications Toolbox (, , ) , (, , , ) , (). , , (). SQL 166 Motorola Motorola

5* 6* 7 8 9

10*

Database Toolbox, ver 3.0 Datafeed Toolbox, ver 1.5 Dials & Gauges Blockset, ver 1.2 Embedded Target Infineon C166 Microcontrollers, ver 1.1 Embedded Target for Motorola HC12, ver 1.1 Embedded Target for Motorola MPC555, ver 2.0

11 12

13

14

15*

1.1 () / 16 MATLAB Embedded Target for OSEC/VDX, ver1.1 Embedded Target for Texas Instruments C2000 DSP, ver 1.0 Embedded Target for Texas Instruments C6000DSP,ver2.1 Excel Link, ver 2.2 Extended Symbolic Math, ver 3.1 Filter Design HDL Coder, ver 1.0 Filter Design Toolbox, ver 3.0 Financial Derivatives Toolbox, ver 3.0 Financial Time Series Toolbox, ver 2.1 Financial Toolbox, ver 2.4 Fixed-Income Toolbox ver 1.0.1 Fixed Point Toolbox, ver 1.0 OSEC/VDX Texas Instruments Texas Instruments MATLAB Microsoft Excel HDL- , , (, 1, 2, 4 ; ) " " GARCH- (General Autoregressive Conditional Heteroscedasticity)

17**

18*

19 20 21** 22* 23* 24 25

26 27**

28 29

Fuzzy Logic Toolbox, ver 2.1.3 GARCH Toolbox, ver 2.0.1 Genetic Algorithm and Direct Search Toolbox ver 1.0.1

30**

10

1

1.1 () / 31 32 33* MATLAB Image Acquisition Toolbox, ver 1.5 Image Processing Toolbox, ver 3.2 Instrument Control Toolbox, ver 2.9 LMI Control Toolbox, ver 1.0.9 Link for Code Composer Studio, ver 1.3.1 Link for ModelSim, ver 1.1.1 MATLAB Builder for COM, ver 1.1 MATLAB Builder for Excel, ver 1.2 MATLAB Compiler, ver 4.0 MATLAB Report Generator, ver 2.0 MATLAB Web Server, ver 1.2.3 Mapping Toolbox, ver 2.0.2 Model Predictive Control Toolbox, ver 2.0 Model-Based Calibration Toolbox, ver 2.1 Mu-Analysis and Synthesis Toolbox, ver 3.0.8 Neural Network Toolbox, ver 4.0.3 (, , , . .' IEEE-488, HPIB, VISA , (Linear Matrix Inequality, LMI) MATLAB (IDE) Texas Instruments (Code Composer Studio, CCS) MATLAB ModelSim MATLAB - MATLAB Excel m- , C++ (RTF, HTML, XML, SGML), Web-, MATLAB- ,

34

35

36** 37 38 39* 40*

41 42* 43* 44 45

46

11

1.1 () / 47** MATLAB Toolbox, ver 1.0 (OLE for Process Control, OPC) , Simulink - Simulink , Real-Time Workshop, , , , , ( ) Simulink, . DSP Blockset (Digital Signal Processing, DSP) Simulink Simulink , Simulink Simulink

48*

Optimization Toolbox, ver 3.0 Partial Differential Equation Toolbox, ver 1.0.5 RFBIockset, ver 1.0 RF Toolbox, ver 1.0 Real-Time Windows Target, ver 2.5 Real-Time Workshop, ver 6.0 Real-Time Workshop Embedded Coder, ver 4.0 Robust Control Toolbox, ver 2.0.10 Signal Processing Blockset, ver 6.0

49

50** 51** 52 53* 54*

55

56*

57 58 59 60 61**

Signal Processing Toolbox, ver 6.0 SimMechanics, ver 2.2 SimPowerSystems, ver 3.1 Simulink Accelera-tor, ver 6.0 Simulink Control Design, ver 1.0

12

1 1.1 ()

/ 62*

MATLAB Simulink Fixed Point, ver 1.0 Simulink Parameter Estimation, ver 1.0 Simulink Report Generator, ver 2.0 Simulink Response Optimization, ver 2.0

Simulink ( Fixed-Point Blockset) Simulink Simulink , ( Nonlinear Control Design Blockset) Simulink - , , , Stateflow Maple ( ) VRML (Virtual Reality Modeling Language)

63** 64* 65*

66** 67 68* 69* 70* 71 72*

Simulink Verification and Validation, ver 1.0 Spline Toolbox, ver 3.2.1 Stateflow, ver 6.0 Stateflow Coder, ver 6.0 Statistics Toolbox, ver 5.0 Symbolic Math Toolbox, ver 3.1 System Identification Toolbox, ver 6.0.1 Virtual Reality Toolbox, ver 4.0 Wavelet Toolbox, ver 3.0 xPC Target, ver 2.5

73*

74*

75

76

xPC Target Embedded Option, ver 2.5

2

2.1. MATLAB 7 MATLAB 7 , Command Window ( ), Command History ( ), Workspace ( ) Current Directory ( ). , . , (. 2.1), " ". , , . ( Undock () 1 3 . ), . , Command Window ( ). , . . doc, help lookfor. , ( , Desktop ( )). Workspace ( ) , . ( Name ()), ( Value ()) ( Class ( )). whos. Workspace ( ) .

14

2

, - Array Editor ( ). , 7- . Workspace ( ).kAMATLAB File Edit Debug Desktop Window Help S ot us \f} H w to A d i>] W a' N w : . . h rc t o d h ts e s=3in(0.5) c=cos (0.5)5*S+C*C ans = 1

5=sin(0.5) c=cos (0.5)

s*s+c*c

. 2 . 1 . MATLAB 7

Command History ( ) , , Command Window ( ) . , . . File () (. 2.2) . (New ()) (Open ()). Close Command Window ( ) Command Window ( ). (Save Workspace As ( )) (Import Data ( )).

15

Open... ; Ctrl+O Clcse Command Window Importbata... , , : Save Workspace As.., Ctrl+S Set Path.., Preferences... Page Setup.,. Print... ." :; ; ' : ; ' 1 C:\...p5\work\array2vec.rn 2 H:\,..AB7\work\untitled.m 3 C:\MATLAB6p5\wcrk\diff2.m 4 C:\MATLAB6p5\workl\test.rn Exit MATLAB Ctrl+Q . 2.2. File

Set Path ( ) , , . Preferences (). . . , . , , . Edit () (. 2.3) (Cut ()), ( ()), (Paste ()), Paste Special ( )) (Delete ()) . Select All ( ) . Undo () Redo () , , . Find () , , . . Clear Command Window ( ),

16

2

Clear Command History ( ) Clear Workspace ( ) . , Preferences ().

Ud no Paste Paste Special... Select All Delete Find... ; Find Files... Clear Command Window Clear Command History Clear Workspace

Ctrl+Z

Alt+W Ctrl+Y

Ctrl+D

. 2 . . Edit

Debug () . 4.9. MATLAB 7 View (). Desktop ( ) . 2.4. Undock Command Window ( ) Command Window ( ) . Desktop Layout ( ) . (Default), . 2.1, (Command Window Only). History and Command Window ( ) . All Tabbed (Co ) (. 2.5). , . MATLAB 7 , Save Layout ( ). , ,

17

. , . . , Organize Layouts ( ). Desktop U d c C m a d Wn o n ok o mn i d w Desktop Layout S v Layout... ae Organize Layouts... Cm ad W d w o mn i o n vertcat All rows in the bracketed expression must have the same number of columns. a=['abcdefghijk';'ABCDEFGHIJK1;'1234567890 '] a = abcdefghijk ABCDEFGHIJK 1234567890 >> whos Bytes Class Name Size 3x11 66 char array a Grand total is 33 elements using 66 bytes

, . . -: b=['ABCDEF' 42345'] = ABCDEF12345 whos Name Size Bytes Class b lxll 22 char array Grand total is 11 elements using 22 bytes

, MATLAB , ( 2.4). 2.4. =[]

;

b=char([ ]

b = c=uint8 ( [

=

whos Name b

25

Size 0x0 0x0 0x0

Bytes 0 0 0

Class double a r r a y char array uint8 array

Grand total is 0 elements using 0 bytes

, .

2.4. , MATLAB IBM- , double. , 8 [10~308, 10 + 3 0 8 ]. 1617. , , , , 16 . MATLAB . . . 9 , 4 , . (. 2.1). 2.1 = 123456789 = 1.2346+009 =1234567890 = 16.6667 =123456789 = 1.6667 =100/6 = 1.0000-004 =10/6 = 0.0010 =1/10000 =1/1000

26

2 2.1 ()

X = 0.0167 =1/ X = 0.1667 =1/60

= 166.6667 = 1.6667+003 =10000/6 =1000/6

= 123.4568 =1234.56789 =123.456789

1.2346+003

, short (). (. 2.2). 2.2

= 1.4142

format

short

format short eX

x=sqrt(2)

X =

1.4142e+000 long

formatX

format long eX

! ! j x=

1.41421356237310 format rational x 1393/985

1.414213562373095e+000

. 2.2 . . , . ( . 2.2), Command Window ( ). File (), Preferences (-

27

) Command Window ( ) . 2.9. Ujt. Preferences

S e ea Gnrl IB-fonts |'-Keyboard * Indenting j C m a d History o mn :t> Editor/Debugger [ Hl ep ' -Web Current Directory Workspace -Array Editor UD GIE-Figure Copy Template

mCommand Window Preferences Text display j Numeric format: | short Numeric display: Display- I ~ Wrap lines I Limit matrix di1

*" |

eighty columns

Number of lines : Accessibility | rational

ndow scroll buffer:

00CHJ

Arrow keys navigate instead of recalling history

;

. ' :

.

/

- '

.::

,

O

K

C

a

n

c

e

l

A

p

p

l

y

H

e

l

p

. 2.9.

short g long g . ( 2.5). 2.5. s h o r t format short g x=sqrt(2) x = 1.4142 x=sqrt(2000000) x = 1414.2 1

28_ = 1.4142+010 x=sqrt(2e+20)

2

bank , , : format bank =1000 1000.00

+ , : format + =[-1 2 - 3 4 -5] ;

=

hex : format hex 1 = X= 3ff0000000000000

, . . MATLAB . sprintf, . . MATLAB . . , . . . Numeric display = compact Preferences () Command Windows ( ) . . 2.9. Numeric display = loose.

29

2.5. , , , . MATLAB , , , -. , , .

2.5.1. , MATLAB . , , 0, (inf = i.8e+308) (-inf = -l.8e+308) , double! =1/0

Warning: Divide by zero. x = Inf y=-l/0 Warning: Divide by zero. = -Inf

, , , , " ", : w=exp(710) Inf w =

, , , 0 0, NaN ( . Not a Number " "): z=0/0 Warning: Divide by zero, z = NaN

3

2

: x=z+l X = NaN

( 2.6): eps ; reaimax ; reaimin ; pi . ;......,.............................................,...............................,......... : 2.6. eps 2.2204 4 604 9250313e-016 reaimin 2.225073858507201-308 reaimax 1.797693134862316e+308 pi 3.14159265358979 ans = ans = ans =

j |

ans =

eps . .

2.5.2. , , .

31

i, j : x=1.5-0.5i 1.5000 - 0.5000i y=1.5-0.5j 1.5000 - 0.5000i = =

"" i j - , , "" : -1; x=i*a = 0 + l.OOOOi = 0 + l.OOOOi z=a*i 0 + l.OOOOi z= y=(i) *;

i j , - , :

= 1

, , : z= 4.1500 - 0.0500i z=(1.5-0.5i)42.5+0.8j)

_32

2

real imag : real(z) 4.1500 imag(z) -0.0500 ans = ans =

complex : complex(1,-1) 1.0000 - l.OOOOi ans =

conj : conj(z) 4.1500 + 0.0500i ans =

, : = 4.1500 - 0.0500i =' = 4.1500 + 0.0500i x=4.15+0.05i'

:z = + i = -1

= ( c o s cp + i2 2 2

-

sincp).

( = + ), -, (,). abs angle ( ). , , , , . . , MATLAB , " ".

33

2.5.3. , , :a 2 2 x 2 + a 2 3 x 3 = b2; = . (2.1)

, - , - , (2.1) :* = .

(2.2)

(2.2) " 1 : = -^. (2.3) - (. . 1) , : = 1 = 4 5 6 2 3 =[4,5,6] =[1 2 3]

, MATLAB -, Tab size () (. Preferences | Command Window | Keyboard & Indenting ( | | )). 4. - (. . 1) , : b = 1 4 2 =[1; 4; 2]

34

2

, : =[1 -1 1; 2 0 2; 0 1 1] =

1 20

-

1 1 0 1

2

1

(2.3) , , ( 2.7). \ 2.7. x=inv(a)*b = 1 1 1 whos Name a b x Size 3x3 3x1 3x1 Bytes Class 72 24 24 double array double array double array

I ;

Grand total is 15 elements using 120 bytes

, , . , MATLAB 1 (1), (2,3). MATLAB . , : z(2:5)=3

MATLAB . zeros ones .

35

eye . rand randn. , , , , , , . 2.8 . 2.8. a=zeros(5) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .0

.0 b=zeros(3, 4) b = 0 0 0 0

0 0

0

0

0 0 0 c=ones([2 3]) 1 1 d=eye(4) d = 1 0 0 1 0 0 1 0 1 1 1 1

0 0 0 1

0 0 0 0 e=rand(4) 0. 9501 0.2311 0.6068

0.8913 0.7621 0.4565

0 .8214 0 .4447 0 .6154 0 .7919

0 .9218 0 .7382 0 .1763 0 .4057

0.4860 0.0185 f=randn(4)

36f = -0.4326 -1.6656 0.1253 0.2877 -1.1465 1.1909 1.1892 -0.0376 0.3273 0.1746 -0.1867 0.7258 -0.5883 2.1832 -0.1364 0.1139

2

( 2.9). : 2.9. g=[e;f] g= 0.9501 0.2311 0.6068 0.4860 -0.4326 -1.6656 0.1253 0.2877 0.8913 0.7621 0.4565 0.0185 -1.1465 1.1909 1.1892 -0.0376 0.8214 0.4447 0.6154 0.7919 0.3273 0.1746 -0.1867 0.7258 0.9218 0.7382 0.1763 0.4057 -0.5883 2.1832 -0.1364 0.1139

- bikdiag. , MATLAB . Fortran. MATLAB , ( 2.10). , , , for ( : : ). 2.10. = 0 y=sin(x) 0.20000.40000.6000

=[0:0.2:1]0.8000

1.0000

= 0.1987 0.3894 0.5646 0.7174 0.8415

37

, .

2.5.4. MATLAB , . 2.3. 2.3 + ; ; * ( ); ; : / = *~1 = * i n v ( B ) (, ) / \ \ \ = "'* ( ) . \ ( ) ; ' '

* /

38

2

2.3 ()

&

; ; ; ;

I

> >= < h

, , , ( 2.15).

2.15. =[1 2 3 4 ] ; =[1 2 2 4] ; p l o t (, , ' s ' )

47

. 2.12.

. 2.12. ,

( t i t l e ) , ( x l a b e l , y l a b e l ) ,

( grid on) ( legend). ( 2.16, . 2.13).I 2.16. =0:0.2:6.28; yl=sin(x); y2=cos(x); legend('sin', 'cos', 4) :

plot(x,yl,'-\,2, ' .') ylabel('y-axis')

48 xlabel('x-axis') grid on title('Function sin & cos''

2

:: -; J:' ; ; .* /

7^ ,/

\ . \ \*

4

0.5' : > > . ...

(

.

1 1

-

0

.

5

i n O S.::,:.": : 1 , . 2

, . :

* 3 4 5 6 7 xas -x iJ

\ \

. 2.13. ,

. legend ( ) : -1 , ; , ; 1 ( ); 2 ; - ; 4 . , , . MATLAB piotyy, ( 2.17). , , , (. 2.14). .

49:

2.17. =0:0.1:6.28; y2=sin(x)+cos (x); plotyy(x,yl,x,y2)

yl=sin(x);

. 2 . 1 4 .

, . subplot, , ( 2.18):subplot(fow,col,cur);

(row) (col). (cur) , plot (. 2.15).; 2.18. x=0:0.2:2*pi; yl=sin(x); y2=cos(x); y3=yl.*exp(-x); y4=yl.*2; ;

50 subplot(2,2,1);plot(x,yl); subplot(2,2,2);plot(x,y2); subplot(2,2,3) ,-plot(x,y3); subplot(2,2,4);plot(x,y4);

2

. 2.15.

2.6.2. (,9) (. 2.16) -, , 9, . - . . , : = - (1 + coscp).

, , plot polar ( 2.19).

51

. 2.16.

2.19. phi=0:0.1:2*pi; polar(phi,ro) title('')

ro=5*(1+cos(phi));

=5 . 2.17.: 90 1 0

120 30

- - ) , j- -t I

240 270

. 2.17.

52

2

2.6.3. , . loglog ( ), semilogx ( ), semiiogy ( ) 2.20, . 2.17. ! 2.20. ;.." ..... ... ..... ..~.~ =0:10:100; =(); semiiogy(,) ; ;

. 2.18.

2.6.4. p l o t f p i o t , y=f () ( x i , x 2 , . . . ) ( y i , y 2 , . . . ) . : fplot(@name_fun, [limits]) fplotCname fun', [limits])

53

name_fun, . limits [xmin xmax] , [xmin xmax ymin ymax]. , . :fplot(@sin, fplot('sin', [0 2*pi]) [0 2*pi])

plot fpiot , . fpiot , 0,2%. , fpiot , 1:fpiot(@sin, [0 2 * p i ] , 0.05)

, 5%. N , fpiot N+I . :fpiot(@sin, fpiot(@sin, [0 2 * p i ] , 0 . 0 5 , 20) [0 2 * p i ] , 20, 0.05)

, plot, , . . . 2.19:f p i o t (@sin, [0 2 * p i ] , 0.05, 's')

fpiot , :[ ] = f p i o t ( @ s i n , [0 2*pi]);

f, fpiot, , , y=f (x,ai,a2). :fpiot('name_fun', [xl x 2 ] , 0 . 0 5 , 20, a l , a2)

54

2

. 2.19. , f p l o t , fplot, . ( 2.21), . 2.20.

100

1 0

50

05 .5 1 >/ i -5 1 1 1 0 "

" i

0 2 0 4 0 6 0 8 01 . . . . . 0 0 0 0. 2 . 2 0 . f p l o t

: 2.21. f p l o t subplot(2, 2,1) , fplot(@humps, [0 1]) f = inline Cabs(exp(-j*x*(0:9))*ones(10,1)) ') ; subplot(2,2,2), fplot(f,[0 2*pi]) subplot(2,2,3), fplotC[tan(x),sin(x),cos(x)]',2*pi*[-l 1 - 1 1 ] ) subplot(2,2,4), fplot('sin(l ./ x) \ [0.01 0.1],le-3)

55_ i

2.6.5. , , . Windows , , , . MATLAB Figure MATLAB . , . , (Figure 1, Figure 2 . .) ( handle):hGWl = p l o t ( , , . . . )

. , . , . gcf ( . get handle to current figure ). , , , ( . get handle to current axis axis ()), ( . get handle to current object ) . . 7. , , : ; fig; MATLAB .3 899

56

2

. 2.21 . Ste] Ve | lw Insert | T os | ol Nw e F ue Toolbar/;: gr i X Lbl ae - Edit Plot C m r T ob r : Y L b l a ea o l a Open.,, : ae Z o In om . : ..gut;. Plot Edit T ob r o l a' Co e ls Z o Out om Title P' : Fg r P l te i ue aet S v ...'.:' ae a n Cp oy Lgn eed Plot Bo s r r we S v As... ae . ' Rtt 3 oae D : Clra o br o Po et E io r p ry dt r G n r t M-Flle... e eae D t C ro aa us r : ' C actAII le er Import Data... Sel Ctrl+A Ln : . . ie Rstlw . e eV e S v W r s a e As... C p Fg r ae ok p c o y i ue Ar w ; ro O to s pi n > Preferences... C p Options... oy T x Ar w e t ro Pin to A e xs Export Setup... np o a o t Fg r Properties... i ue D u l Ar w S a T L y u Grid o b ro e P g Setup... . - : x s Properties...: ae V w L y u Gi i e a o t rd Ae T xB x et o S at Align and Distribute mr R ca g etn l e Print Setup...' > C re t O j c Properties... ur n be t Align Distribute T o ... ol Ei s lp e C l r a .. oom p. Print Preview,.. Print.. Fn Files... id Ae xs Align . . C a Fg r ' l r i ue e Light Distribute ac t n i ... : .. C a C m ad W d w l r o mn n o e i : B s Fiti g D t Statistics aa C a C m a d Hsoy : l r o mn i t r e C a W r s a e .':' l r okp c e. 2.21.

File () (Close ()), (Open ()) , Property Editor ( ) (New Figure ( )). (Save ()) (Save As ( )) , (Export Setup ( )). , MATLAB, . 2.9. 2.9 1 2 3 Enhanced metafiles Bitmap files EPS (Enhanced PostScript) files emf bmp eps

57 2.9 ()

4 5 6 7 8 9 10 11 12 13 14 15

EPS Color files EPS Level 2 EPS Level 2 Color Adobe Illustrator files JPEG images TIFF images TIFF no compression images Portable Network Graphics files Paintbrush 24-bit files Portable Bitmap files Portable Graymap files Portable Pixmap files

eps eps eps ai jpg tif tif png pcx pbm pgm ppm;

Generate M-File ( -) . , . , , . 2.10, MATLAB :function createfigure(xl, yl, y2) %CREATEFIGURE(XI,Yl,Y2) % XI: vector of x data % Yl: vector of data % Y2: vector of data % Auto-generated by MATLAB on 03-Sep-2004 22:35:57

%% Create figure figurel = figure('PaperPosition1, [0.6345 6.345 20.3 15.23], ... 'PaperSize', [20.98 29.68]); %% Create axes axesl = axes(... 'YColor1, [0 0 1], . . .

58YTick',[-1 -0.5 0 0.5 1 ] , . . . ' Parent' , figure!.) ; axis(axes1,[0 7 -1 1] ) ; hold(axes1,'all'); %% Create plot plotl = plot(xl,yl,'Parent',axesl); %% Create axes axes2 = axes ( . . . 'YColor',[0 0.5 0], . . . 'YAxisLocation','right',... 'YTick', [-2 -1 0 1 2]., . . . 'Parent',figurel); axis(axes2,[0 7 -2 2 ] ) ; hold(axes2,'all'); %% Create plot plot2 = plot(xl,y2,'Parent',axes2);

2

xl, yi 2, . , 34 . , , , . , , . Import Data ( ) Save Workspace As ( ) . Preferences () MATLAB, . File () . Export Setup ( ) , (Size), (Rendering), (Font) (Lines). fig.

59

File () , (Print ()). Page Setup ( ) , . (. 2.22) (Use manual size and position ( )) ( (), Left (), Width () Height ()) (Units ( )). (Use screen size, centered on page ( , )). IPage Setup - Figure 1 Size and Position | paper j Lines and Text j A e and Figure xspMode ; j use screen size, centered on page I > dbstop array2vec 2 dbstop ai:ray2vec 3 >> dbstatus Breakpoints for array2vec are on lines 2, 3.

ill

^

. 4.14.

( , ) , >>. . ( Debug | Step ( | )) dbstep. dbstep . dbstep : dbstep 5

, Debug | Step In ( | ) ( ) Debug | Step Out ( | ) ( +).

158

4

. , . , View | Workspace ( | ) whos. , , . dbup ( ) dbdown ( ). . dbstack. , , , , . dbquit . Debug | Exit Debug Mode ( | ). M-Lint, Tools | Check Code with M-Lint ( | M-Lint). BELL Laboratories. 30 lint, , . lint , , , . , , , , . . , , . lint Language Interpreter ( ). MATLAB 7. , , . . M-Lint . . 4.15 polyline, .

Editor - H:\MATLAB7\work\polyline.m File E i T x C l T os De^ug Desktop: Wn o H l dt e t el o l i d w ep QSa i *- > 1 | unction polyline(varargin) f 2 for k=l: length (varargin) 3x(k)=varargin(k) (1) ; 4y(k) = v a r a r g i n ( k } (2) ; 5 * end 6a x i s {[min (x) max(x) min(y) max (y) ])7 " Plot(x,y) VM-Lint Code Check Report File Edit View G Debug Desktop Window Help o ^ . . _ ^ . ...^ . .

159

M-Liiit Code Checker ReportReport for f i l e H: \MATbAB7N-woik\polyline

H: \MATLAB7\work\polyline.in

;3: Array Consider messages 4: Array Consider

T

x T is constructed using subscripting. preallocating for speed T y' is constructed using subscripting. preallocating for speed

. 4.15. M-Lint

VM-Lint

Code Check Report

File Edit. View G Debug Desktop Window Help. o

M-Lint Code Checker ReportReport for f i l e H: \ l i T L B 1 \ w Q r k\ p 1 l i n e "A A _

H:\MATLAB7\work\polyline. m

4 messages

1^ Invalid character " Function name ' olyline' will be known to MATLAB X_ .by its file name: TpolylineT. ._ Array f x* is constructed using subscripting. 3 Consider preallocating for speed 4j_ Array T y T is constructed using subscripting. Consider preallocating for speed

. 4.16.

160

4

, M-Lint , 3- 4- . M-Lint , . ( polyline noiyiine) (. 4.16).

4.11. MATLAB 7 , Profiler, , . . . , , , . Profiler : MATLAB ( Desktop | Profiler ( | )), ( Tools | Open Profiler ( | )), Command Window ( ) profile viewer. Profiler () Run this code ( ) (. 4.17) Start Profiling ( ). , bench.m, . 4.17 , . Function name ( ) , Calls () , Total Time ( ) , , Self Time ( ) , , . . Function name ( ), , (. 4.18). (. 4.19). ode45.m.

161

Start Prof iing j Run this code: j bench

jrj

Profile tlrocMO sec

! Profile Summary\ Generated ll-Sep-2004 \ Function name 18:54:26 Calls Total Time 1 :247

Self lime* 4.703 s 0.547 s 0.031 s 0.000 s 0.156 s 0.000 s 0.063 s 0.203 s 0.031 s

Total Time Plot (dark band = self time) H l i I iI

bench ode45 newplot : newploOObserveAxesNextPlot graphics rivate\clo ! barh membrane reemat iimfun\privale\iitrp4JProf Jer i Commeind window |1

i 6.797 s : 0.984 s 0.422 s 0.391s 0.359 s 0.219 s ! 0.203 s

.. i ', ; '' j i.' 1; ; ;

47 ;47 2

2166 j 0.203 s 1080 0.188 s

. 4.17. Profiler

'- + 4 0:\Start Proflbfl I RLBI W code J bench

Ml ^1 eProffletimeMOsec

Profile Summaryj denerated 1 l-Sep-2004 21:06:32 Fiuittion name i ancestor ancestor>isatvpe automesh j axes (Opaque-function) I axesclieck 60 1207

Calls Total Time 0.063 s 0.031 s Os 0s 0.016 s 0.031s 0.016 s

Self Time*

i Total Time Plot (dark band = self time)

function is recursive function is recursive 0.000 s 0.000 s 0.016 s 0.016 s 0.016 s | | i

80 5 3 1

Sa x k1 ffid?>LocSetLiniite

. 4.18.

162

4

_] : J*J prontethiaiOsec

:

Start Profiling I Run tins *, bench

j ode45 (2 calls, 0.984 sec) j Generated ll-Sep-2004 21:09:54 ' M-function in file H:\MATXAB7\toolboxWitlab\funfiin\ode45.m ! [Copy to new.window.for comparing multiple runs]I Refresh J

P Show parent

files

F Show busy lines

P Show child files

P Show M-Lint results P Show file coverage P Show file listing Parents (calling functions) ! Filename j File Type ' beach1

Calls

! M-function I 2

Lines where the most time was spent % Time Time Plot

; Line Number f4 S t a r t j .

Code "' ': ' ;' : .!

Calls _ . Time :.':' . ; ' " -i"1'.1:^ /..' . .

:

__

Profiler 1 Command VWKlow:;.'

" .. , '-

. 4 . 1 9 . ode45.m

* _ i : # i *Start Profahg j RuntNs code: j bench

Lines where the most time was spent Total Time

....-dCalls Time 23.8% 9.5% 6.3% 6.3% 6.3% 47.6% 100%

bprotietiiwlOsec -" _ J

Line Number 434 326 505 325 ' 324 Other lines & overhead Totals

Code jyout_now t(:,4) == [ntrp45(tref, t , y, [ ] . . feval(odeFcn,t+hA(3),..

Time Plot

1080 0.234 s 1326 0.094 s2

I j

solvsr_o jtput (:.3) = > lower('Happy New Year - !') ans = happy new year - ! upper('Happy New Year - !')

ans = HAPPY NEW YEAR - !

MATLAB 7, , "" "": lower('') ans = upper('') ans =

,

5.6. double num2str i n t 2 s t r . ( 5.27).7 899

184

5

I ans = 3.1416 whos Name ans

'. .'....num2str(pi)

.1...

. ' < * < , . U l . K K d t l ' I . M k . k

l t . O K

M

'

"

-

'

'

!

> * '

Size 1x6

Bytes , 12

Class char array

Grand t o t a l i s 6 elements using 12 bytes

, , double ( 5.28). ' ' *:

5.28. num2str(pi,10)

I

ans = 3.141592654 num2str(pi, 20) % 17 double ! ans = 3.1415926535897931

num2str ( 5.29).! 5.29. '.. . ......\.:;;.-......-..:-.,. = 0 + 1.4142i num2str(x,10) ans = 0+1.4142135621 whos Name ans x Size 1x14 lxl Bytes 28 16 Class char array double array (complex) x=sqrt(-2) :

Grand total is 15 elements using 44 bytes

185

, num2str ( 5.30) , sprint f.; " - : - -

; 5.30. num2str(pi,'pi=%6.2f ) ans = pi= 3.14 whos Name ans Size 1x9 Bytes 18 Class char array

Grand total is 9 elements using 18 bytes

i n t 2 s t r . ( 5.31).I 5.31. I. .................'..-...... ;....i i'..

;J

x = [ [ 0 . 1 0.5

0 . 8 ] ; [ 1 0 0 . 1 100.5

100.8]]

0.1000

0.5000

0.8000 100.8000

100.1000 100.5000 int2str(x) ans = 0 1 1 100 101 101 whos

Name Size Bytes Class x 2x3 4 8 double array ans 2x13 52 char array Grand total is 32 elements using 100 bytes x=NaN; int2str(x) ans = NaN x=Inf; int2str(x)

186 ans = Inf x= 2.2251e-308 0 x=realmax 1.7977e+308 int2str(x) ans = int2str(x) ans = x=realmin

5

17 97 693134 862315700000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000 length(ans) 309 ans =

i n t 2 s t r , ( 532). 5.32. int2str X =

x=sqrt(-2)+5.8 5.8000 + 1.4142i

6

int2str(x)

ans =

double str2double str2num. ,

187

, , , , , , , i j , ( 5.33)...... | 5.33. Sl=str2double('3.14159265') 3.1416 Sl=str2double( 4,000, 000') 1000000 Sl=str2double('le6') 1000000 SI = 1.0000 - 2.0000i . SI = Sl=str2double('l-2j') SI = ; :

S1 =

str2num , ( 5.34).j 5.34. > >; S=['1 2' 3 4'] S = 1 2 3 4 > >: str2num(S) ans = 1 3 >> whos Name S ans Size 2x3 2x2 Bytes 12 32 Class char array double array 2 4 :

Grand total is 10 elements using 44 bytes

188

5

, . . ( 5.35).! 5.35. I

! . . . =

:. .

.. .. .~

~. .

. .

x=str2num('1-2j ') 1.0000 - 2.0000i

y=str2num('1 -2j') = 1.0000 whos Name x Size lxl 1x2

0 - 2.0000i

Bytes 16 32

Class double array (complex) double array (complex)

, . , , dec2bin, dec2hex, dec2base, bin2dec, hex2dec, base2dec, hex2num. (dec . decimal) (bin . binary) (hex . hexadecimal) , (base) 2 36. 2 "" (. two "" to "" ). double . dec2base itoa C++. , 2 36, , 10 . 10 26 36. dec2bin(N) , 2 5 2 , ( 5.36).

189

1\'-5:* ' .

:. . .

"""""\;

dec2bin(792) %

ans = 1100011000

dec2bin(N,k) , . , : dec2bin(792,5) % 5 ans = 1100011000 dec2bin{792,12) % 12 ans = 001100011000

dec2bin , , : =[148 1023;525 7 9 2 ] ; dec2bin(a) %

ans = 0010010100 1000001101 1111111111 1100011000

dec2hex ( 5.37). : :

5.37. dec2hex(792) %

ans = 318 dec2hex(792,5) % 5 ans = 00318 =[148 1023;525 792]; dec2hex(a) %

190 ans = 094 20D 3FF 318

5

dec2base. ( 5.38).: ::

: 5.38. ; ;........

I.i.fMMHHI

dec2base(792,5)

%

ans = 11132 dec2base(792,5,8) % 8 ans = 00011132

bin2dec, hex2dec base2dec , double ( 5.39). i 5.39. bin2dec('1100011000')

!

ans = 792 hex2dec('318')

ans = 7 92 base2dec('11132',5)

ans = 792

, ( 5.40).

" : ----

191' '''

I 5.40. bin2dec(['101100';'001101']) 44 13 whos Name ans Size 2x1 Bytes Class 16 double array

I

ans =

double 8 16 , , IEEE 1 , 11 52 . , , IBM- 400921FB54442D18.

hex2num ( 5.41). .'--- - .. .......! I 5.41. I format long hex2num('400921FB54442D18') 3.14159265358979

ans =

hex2num 16 , . mat2str str2mat ( 5.42).

.! 5.42. :;....,.,;!.... ....:

192 = [1 2;3 4] z= [1 2;3 4] X

' 5

z=str2mat [1 2;3 4] ')

whos Name z Size 2x2 1x9 1x9 Bytes 32 18 18 Class double array char array char array x = [ l 2 ; 3 4]

Grand t o t a l i s 22 e l e m e n t s u s i n g 68 b y t e s

.

5.7. (sprintf, sscanf) MATLAB , . , , . . (ai, a2, ...) fmt s sprintf:[s,s_err] = sprintf(fmt,al,a2,...)

sprintf , s_err. , , :s = sprintf(fmt,al,a2, . . . )

, , : sprintf (' %ld\pi) ans = 3.141593+000 sprintf('%lf',pi)

ans = Empty string: l-by-0

793

%if, , MATLAB s_err=Invalid format. fmt , , % , , . , , 5 :%[][][.][{b,t}]a

(%) ().

(-, +, " ", #, 0). - . . + ( ). " " () ( ). # , . , . , f, e , ( , 0). , g G, . . , ( 5.43).

123

s p r i n t f ( ' % - 5 d %+5d %05d',123,123,123) i +123 00123 sprintf('%x %#X\123,123)

ans =

194 ans = 7b 0X7B s p r i n t f ( ' % d',123) 123 s p r i n t f ( ' % d',-123) ans = -123 ans =

5

, , (), . , , , : sprintf (' %10. 6f,pi) 3.141593 ans =

, , . . 5.1. 5.1 % %d % % %f % %G % %s % % % ( ) ( ) a f AF

195

%d, %u, %o, % % x ( double). , MATLAB :>> sprintf('%d',pi) ans = 3.141593e+000

% : d 3 sprintf('%d',round(pi)) ans =

t, -, IEEE. %, % %. , IBM- ( 5.44).

sprintf{'%bx',pi) %

ans = 182d4454fb210940 format long hex2num('400921FB54442D18') 3.14159265358979 sprintf C%bx',l) \ ans = 000000000000f03f hex2num('3ff0000000000000') 1 sprintf C%tx',l) ans = 3f800000 ans =

ans =

196

;

5

, fmt , , : sprintf('rra=%10.8f',pi) ans = =3.14159265

, (. 5.2). 5.2 \ \f \ \ \t \\ \' % % (Backspace) (Form feed) (New line) (Carriage return) (Horizontal tab) (Backslash) ) (%)

s, sprintf, sscanf: = sscanf(s,fmt)

fmt. , (. . 5.1), , (, , . .), . : , *, s; %hd, %id %ig, (%hd), (%id) (%lg);

197

%[sis2...], s, ; % [~sis2 . . . ], s, . , , ( 5.45).

| 5.45. i lnlMMII Illllttl, Milt ^ll.tLIHIll..!. ';* I I I tl , I i > j , I I I I .. .. I ., . I M I I I I I M I. 1 s = '2.7183 3.1416'; A = sscanf(s,'%f') 2.7183 3.1416

A .

: sl=sprintf('%f ',A) si = 2.718300 3.141600

. "", ( 5.46). ,.........,...,.......,.........,, ,,..,.,,,....,..,..,.,,,.-,... 'I 5.46. 1 ... sl=sprintf('%f',A) ..................... .............................................

I

si '= 2.7183003.141600 s2=sl(l:8) s2 = 2.718300 s3=sl(9:end) s3 = 3.141600

198

5

, , : sl=sprintf ('%-8.4f \A) 3.1416 si = 2.7183

sscanf , : = sscanf(s,fmt,size) [A,count,s_err,nextindex] = sscanf(s,fmt)

5.8. . 5.4 . . . . , , , . , , : "_ _ _". "", "". , , "", - , "". , . , , . , *. . * , . . : (.) , . , , \.;

199

\w , , , . . , ; \w , , , ; \s , ; \s , ; \d , ; \D , , ; [sis 2 .. .s k ] , , ; [~sis2. . .sic] , , .

, , , :[abode] = [-]

, . , '\W*KO\S' , , "", . , , . R, R I , R2 , . , , : (R) * R 0 ; (R) + R 1 ;

(R) ? R 0 1 ; R{n,m} R m ; R{n} R ; R{n,} R ; (R11R2) , RI, R2;

200 "R ; R$ ; \ .

5

, , MATLAB : regexp ; regexpi ; regexprep . : () , ; () ; . 'match', 'start', 'end', 'tokens', ' t o k e n E x t e n t s ' , 'names',

. , : regexp(423_abc 456_78','\d{3}') 1 9 ans =

'match', regexp : regexp('123_abc 456_78','\d{3}','match') 423'

ans = '456'

' s t a r t ' , : [m s ] = r e g e x p ( ' 1 2 3 _ a b c 423' 456J78','\d{3}','match','start')

m= '456' 9

1

201

: = 1 s = 423' '456' 9 [m s]=regexp('123_abc 456_78','\d{3}','start','match')

'end' : m = '123' indl = 1 ind2 = 3 11 9 '456' [m indl ind2]=regexp('123_abc 456_78','\d{3}','match','start','end')

5.47.

s ='abCD_ 123#'; [mat ind]=regexp(s,'\s','match','start';

mat = ind = [mat ind]=regexp(s,'\S','match','start') 'a' ind = 1 mat = 4' ind = 7 8 9 '2' '3' 2 3 4 5 7 8 10 [mat ind]=regexp(s,'\d','match','start') 'b' 'C 'D' '_' '1' '2' '#'

mat =

202 [mat ind]=regexp(s,'\D','match','start') 'a' ind = 1 mat = 'a' ind = 1 mat = ' ' ind = 6 10 '#' 2 3 4 5 7 8 [mat ind]=regexp(s,'\W,'match','start') 'b' 'C 'D' '_' '1' '2' 2 3 4 5 6 10 [mat i n d ] = r e g e x p f s , ' \ w ' , ' m a t c h ' , ' s t a r t ' ) 'b' 'C 'D' '_' ' ' '#' mat =

5

, , ( 5.48). 5.48. | ! : . str='Uapb, , , '; pat=' | op' ; regexp(str,pat) 2 8 17 25 % R % R1 R2

patl='H4\>'; regexp(str,patl) 12 31A

ans = pat3=' uap'; regexpi(str,pat3) 1 regexpi(str,pat4) R %R

ans =

ans = 31 pat2='\> regexprep(s,pat,'EFgh','preservecase')

206 ans = EFGH_efgh regexprep(s,pat,'EFGH','preservecase') ans = EFGH_efgh s='Abcd_aBCD'; regexprep(s,pat,'EFGH','preservecase')

5

ans = Efgh_EFGH regexprep(s,pat,'EFgh','preservecase1) ans = Efgh_EFgh

, .

6

, () , . . , , . , . , , , , : , , , , , . . , - . , , - : ( ); ( ) ( ) ; ( ). , , . ( MATLAB ) . ,, , . , ( -

208

6

CR , LF ). , . , , , . - . , , . , , , . . , 71=3.14159265358979 16 , 8 . , , , . . . , , . 8- 16- , , (PCX, BMP, JPG, TIF . .), , . , , , CR, LF, Tab . . - . . , . , , . , , , , .

209

6.1. fopen, f i d ( ). :f_id = fopen('name_file');

, , , MATLAB. , . - 1 . , , fopen :[f_id err_txt] = fopen('name_file');

err_txt . , . fopen . , , . . 6.1. 6.1 fopen (' n a m e f i l e ' , ' ' ) fopen (' name_f i l e ' , ' r t ' ) fopen (' n a m e f i l e ' , ' r + ' ) , , ( ) , , ( ) .

fopen ( ' n a m e f i l e ' , ' r t + ' )

fopen ('name_f i l e ' , ' w ' )

210

6 6.1 ()

fopen('name file', 'wt' )

. , . , . , . , . . , . ,

fopen('name_file','w+'

fopen('name_file','wt+'

fopen('name file','a') fopen('name_file' , 'at' fopen('name_file','a+ '

fopen('name_file' , 'at+')

C++ ( . binary ): rb, wb . . , . , "" . , , . (truncate) , . , . , , , , , . -

211

, . , , . flash, flashing. w W. IBM- . (stdin), (stdout) , (stderr). MATLAB 0, 1 2 . , . , , , , . fopen , , ( 6.1).; ;- ".-V........................ , ,....,..,

6.1. [name access]=fopen(0) name = "stdin" access = r >> [name access]=fopen(1) name = "stdout" access = a [name access]=fopen(2) name = "stderr" access = a k=fopen('qq\ 'wt+')

212

63

= qq access = wt+ [name access]=fopen() name =

f open :v = fopen('all')

, ( 0, 1 2 ).

6.2. , , . , fciose, . -, , . -, , . fciose :s = fciose(f_id) s = fciose('all')

, . - 1 , . , . fciose .

6.3. , , , . , (end-of-file), f eof:k = feof(f i d ) ;

213

, . , feof, 1.

6.4. "" . , , . , , "" , , . . . f_id , f t e i i :pos = ftell(f_id);

frewind. REWIND, Fortran . frewind (f_id). fseek, C++. : (>)st = fseek(f_id,n,'bof'); st = f s e e k ( f _ i d , n , - l ) ;

(>)s t = fseek(f_id,n,'eof') ; s t = fseek(f_id,n,1);

(>) (> frewind(f_id) a=fread(f_id, [3,Inf]) a = 65 68 48 51 66 13 49 52 67 10 50 53

, . , ,

220

6

double, . . . , . fread "-" - , double. , , * 1=>2 , , ( 6.9). 6.9. -roi5

.^.*

frewind(f .id) ; a=fread(f__id, [3 4],'uchar=>uchar = 65 68 48 51 49 52 66 13 67 10 50 53 whos Name Bytes Class Size 3x4 12 uint8 array f id 8 double array lxl , 1=>1 *1. uchar=>uchar *uchar.

6.4.2. , fwrite; count = fwrite(f_id,array,'type'); f i d a r r a y ,

221

. f_id stdout stderr. , fwrite, . count , array, . array , . ( ) 6.10. -

-

[ 6.10. al='ABCD'; 2='012345'; k=fopen('abc.ra','w'); cl=fwrite(k,al,'uchar') 4 2 = 6 % % % % fclose(k); k=fopen{'abc.m',''); % % % % al

cl = % % 2 c2=fwrite(k,a2,'uchar1)

>> [ c3]=fread(k,inf,'*uchar') = 65 66 67 68 48 49 50 51 52 53 = 10 whos % % % % D % 0 % 1 % 2 % 3 % 4 % 5

%

222Name al a2 a3 cl c2 c3 Size 1x4 1x6 10x1 lxl lxl lxl lxl Bytes 8 12 10 8 8 8 8 Class char array char array uint8 array double array double array double array double array

6

, CR, LF . , ( 6.11).

6

!

1

:

*

...

1 . i

frewind(k) %

a4=fread(k,inf,'uchar=>char': a4 = ABCD012345

, . ( 6.12).

6.1

k=fopen('abc.m','w'); c2=fwrite(k,a2,'char') , fclose(k); k=fopen('abc. m', 'r'); a4=fread(k,inf,'*char'; ABCD012345

>> cl=fwrite(k,al,'char') ,

a4 =

fwrite , ( 6.13).

1

223 '

al='ABCD'; k=fopen('abc.m','w'); cl=fwrite(k,al,'uchar',2) 4 %

cl = % fclose(k); k=fopen('abc.m','r'); a4=fread(k,inf,'uchar') 0 65 66 67 68 % al % ,al % al % % % al

a4 =

6.5. , CR ( OD) LF ( ). , . , (. fprintf) '\. text.txt, :ABCD 012345

, 12 "" ( 43) ODOA.

224

6

6.5.1. fgeti. , . . , fgeti, -1 ( 6.14).! 6.14. ;.... V....... ...... k=fopen('text.txt','rt'); sl=fgetl(k) % % I ;

:

si = ABCD s2=fgetl(k) % s2 = 012345 s3=fgetl(k) -1 % s3 =

.

6.5.2. fgets, . , : ; ; . , , ,

225

. fgets - 1 . 6.15 . ;

/ ,*.!

I

k=fopen('text.txtp,'rt')f sl=fgets(k)

% %

si ABCD s2=fgets(k,3) % s2 = 012 345 s4=fgets(,1) -1 % S4 = s3=fgets(k,5) % , % s3 =

6.5.3. , , , , () : , ; , . , . , fprintf ( 6.16).

f_id=fopen('text.txt','wt'); cl=fprintf(f_id,'%s\n','ABCD')

\ 6.16.

.,.,.......,,,,.....,.,,,% %

; !

226 cl = 5 fclose(f_id);

6

% . %s, , ABCD, 8 . \, , . , fprintf (. 6.1).; view text.txt - Far

41 42 43 44 0D Qfl. 6 . 1 . text.txt

'RBCDJfi

, . . , ( ), fprintf ( newiine ). fprintf . , , 1, , . . : f p r i n t f ( I , ' % s \ n \ 'ABCD 1 ); ABCD

To : fprintf('%s\n','ABCD'); ABCD

fprintf , . , . . , . . MATLAB -

227

. , , : fprintfC%4d',l,2,3,4) 1 2

3

4

%4d . , : fprintf('%d %f %e',l,2) 1 2.000000 , , fprintf. , i i , i2 , , 6.17. 1 il=125; i2=2003; r3=2.3456; fprintf('%5d %d %8.3f\n',il,i2,r3); 125 2003 2.346 i i ( d ), 5 . , , . 2 % 4 . d , . 8 ( f ), 3 . , . , fprintf, MATLAB C++ . , , , -

228

6

. . %, , (w) (.). , . 6.3. 6.3 d i , , f , , , .... F , , , ( ) , ( )

f g

G

s

229

, % , : , ( ). ;

+ ( ); ( ); # ( , ). w, , , . , , , . 6.18 ( ). 6.18.

=1;

=-1; fprintf ('%5d\n%5d\a,b) 16 % -16 % fprintf('% + 5d\n%5d\a,b) +16 % + -16 fprintf('%-5d\n%-5d',a,b) 16 % , + -16 - % fprintf('%- 5d\n%-5d',a,b) 16 % , + -16 % fprintf('%5\%#5',,); 10 0x10 % '

230 f p r i n t f ('%5X\n%#5X\a,a) ; 10 0X10 20 020 00016

.

6

%

fprintfC%5o\n%#5o',a,a); % 0 %

fprintf('%05d',a)

, (%d, %i, %u, %x, %x, %o) , ( 6.19).

(

J

Command Window ( ) MATLAB 6.5. MATLAB 7 f p r i n t f (>>) . , .; '. " , - |

| 6.19. 1.8 z=1.8; fprintf('%d',z) % %d % % % % % % % % % % % fprintf('%x',z) fprintf('%u',z) fprintf('%f',z) fprintf('%',z) fprintf('%E',z) fprintf('%g',z)

1.800000+000 1.800000+000 1.800000+000 1.800000 1.800000+000 1.800000+000

231

, : fix, round,floor c e i l .

MATLAB double 8 16 . , % , %, % ( 6.20). ; 6.20. fprintf('%bx\n%bx',l,-l)

3ff0000000000000 bffOOOOOOOOOOOOO

t . float, "" 8 ( 6.21). " . . . . . . . . . . .

6.21. f l o a t fprintf('%tx\n%tx',l,-1)

3f800000 bf800000

, - "" . , , . , , . C++ , . . , . %io.5s 5 , , 10 .

232

6

C++ , . , C++.( )

, , , ( 6.22).

6.22. i a=l+2*i 1.0000 + 2.0000i b=3+4*j 3.0000 + 4.0000i fprintf ('%4.2f\n%4.2f,a,b) 1.00 3.00 b =

fprintf , ( , newiine). fprintf . , , . MATLAB , ( 6.23).. - " ' : . . . . . . . . . . . . . . . . . . . . . .:

-

\ 6.23. ;....." i .:....... ...........................i.. ;....

..;

\

n=fprintf ( % %bd\l,-l) 'd 1 n = % %bd , 2 n=fprintf ( % %d\l,-l) 'd 1 -In = 4

233

, ( 6.24).11^1 1111'1||''"" '1|"'"'"'''' '" ill ' 'iii i i ' 6.24. =0:0.2:1 = 0 = 0 0.2000 0.4000 0.3894 0.6000 0.5646 0.8000 0.7174 1.0000 0.8415 0 0.1987 fprintf('%4.1f %8.4f\n' ,) 0.0000 0.0 0.2 0.4 0.6 0.8 1.0 0.1987 0.3894 0.5646 0.7174 0.8415 0 0.2 0.4 =[; 0.2000 0.6 fprintf('%g ' ) 0.1 I 1 % 6 0.4000 0.6000 0.8000 1.00001 | |T| 111

% % 2x6

sin(x)]

% 6

('\') , Esc-. . 6.4. 6.4 \ \f \ \ \t (Backspace) (Form feed) (New line) (Carriage return) (Horizontal tab)

: % ; ; Esc-.

234

6

, : % ; % \\ ; ' ' ; \" . , f p r i n t f , ( 6.25).

6.25. m=fprintf('ABCDEFGH IJKLMN1);

ABCDEFGH IJKLMN m m= 15

6.5.4. fscanf, :[a n]=fscanf (f_id, 'format',. m ; )

: a , ; ; f_id ; format , ; m . , f i d format. inf -

235

. , - mxi. , [ q], pxq. 5x5, a.txt, ( 6.26). ,. ,

| 6.26. >> k=fopen('.txt', 'wt'); >> a=inagic(5) 17 23 4 10 11 24 5 6 12 18 1 7 13 19 25 8 14 20 21 2 15 16 22 3 9 % % , % % % 5x5

n=fprintf(, %d \a) 66 fclose(); k=fopen('a.txt', ' r t ' ) ; [b n]=fscanf (k, ' % d \ [5 5]) 17 23 4 10 11 24 5 6 12 18 1 7 13 19 25 8 14 20 21 2 15 16 22 3 9

b

25 frewind(k); [c n]=fscanf(k,'Id1,5) 17 23

% % % 5 (1- )

2 4 10 n 5

3

6

6

whos Namea b

Size 5x5 5x5 5x1 lxl lxl

Bytes 200 200 40 8 8

Class double array double array double array double array double array

n

fprintf fscanf. , , , . % d , . 25 ( ), 66 . , fscanf, , fprintf (. . 6.3). , . %, f % % , g double. %d, %i , double. %*, , , . . 6.27 .! 6.27. frewind(k); [d n]=fscanf(k,'%d %*d')

. 1

d = 17 4 11 5 12 1 13 25 14 21 15 22 9 13

237

% -. s , , , . %s, . , , , ( 6.28).: :

I 6.28. .. .............

k=fopen('.txt','wt'); fprintf(k,'%s\'ABCD 012345' 11 a= ABCD n = frewind(k) ; [a n]=fscanf(k,'%s',2)

%

ans = fclose(k); k=fopen('a.txt', 'rt') ; [ n]=fscanf (k, '%s', 1) a % 1

2

238

6

ABCD012345 = 2 a = ABCD01234 5 = 2 k=fopen('.txt','wt'); fclose(k); k=fopen('a.txt','rt'); [ n]=fscanf(k, '%s') a

%

% % 2

fprintf(k,'%s\n%s','ABCD','012345');

% % 1- % 2 '

6.6. , fprinf fscanf, sprintf sscanf. , , , , , , , , , :[str errtxt] = sprintf('format',al,a2,...); [a n] = sscanf(str,'format',m); % str % str

. 5.7.

6.7. . ferror:msg_err = ferror(f_id) ; [msg_err num_err] = ferror(f_id);

, m s g e r r , num_err .

239

, . :[msg_err num_err] = f e r r o r ( f _ i d , ' c l e a r ' ) ;

6.8. textread ( 6.29), , fscanf , . , . , , . . . textread , , , . , textread , . . " " , -, . textread .! 6.29. t e x t r e a d..

I;

,

k=fopen('.txt','wt'); fprintf(k,'%d fclose(k); a=textread('a.txt') 1 2 3 4 5 ',1,2,3,4,5);

% % 5

a =

MATLAB 7 0. .

, ( 6.30).

240 . 6.30.

6

b=textread('a.txt','%d',2) b -

.

, , ( 6.31).

b = t e x t r e a d ( ' a . t x t ' , '%d': b= 1 2 3 4 5

, , ( 6.32).

f : _ : :

[ 2 4 b51-textreadCa.txt 1 ,bl

%d %d %d

b2 =

=

4 =

5 =

241

* , ( 6.33).[ 6.33. ; _

^

)

[ b 5 ] = t e x t r e a d ( ' a . t x t , ' % d % * d %d % * d % d ' ) 1

1

= = 3 5 = 5

, ( 6.34).| 6.34. b= 1 2 3 4 5 b=textread('.txt', " )

^

MATLAB 7 0. .

, textread, , . 6.3. . 6.5. 6.5 % %d %u %f d o u b l e d o u b l e d o u b l e d o u b l e

242

6

6.5 () %s %q % %[ s l s 2 . . . ] , , , , , , , . , , , . ,

% [A s l s 2 . . . ]

% , , -. - . textread " ", - . . 6.6. 6.6 'buf s i z e ' 'commentstyle' , ( 4096) . : 'delimiter' 'emptyvalue' 'endofline' 'matlab': m- % ; ' s h e l l ' : #; '': - /* */; '++': C++ //

-

243

6.6 () 'expchars' 'headerlines' 'whitespace' , ( 'eEdD') ( '\b\t')

textread , " ". . datal.dat , :Sally Joe Bill Typel 12.34 45 Yes Type2 23.54 60 No Typel 34.90 12 No

fprintf ( 6.35).i 6.35. ' . .......s. ..,..,..;.....,... F i g u r e 1 1 File Edit'view Insert Tools: Desktop Wi dow Help" n

l-'jal;x|. . .. ,

3.5\

4

. 7.11.

-

: 7.12. rectangle('Position',[1,2,5,10],'Curvature', [1,1],... 'FaceCoior','')

12 10 \ i f 4 : 2 ' ; 4 5 6

. 7.12.

291

7.6. Text text ( Text) Axes. outtextxy Borland Graphics Interface: h_Txt=text(0.5,0.5,"Hello, World!');

. . . 7.13.

02 .

.. 04

06 .

, 08 .

. 7.13.

HorizontaiAiignment ( ) verticaiAiignment ( ). left ( ), center ( ), right ( ). . , . 7.14. , Extent. middle ( ), baseline ( ), top ( ), bottom ( ), cap ( ). . 7.15. .

292

7

Left Center Right

. 7.14.

Middle

Ragplinp

Bottom Top cap

. 7.15.

, z: h_Txt=text(,,z,'');

text " ", Text. 7.13 , . 7.13.; 7.13. Text get(h_Txt)BackgroundColor = none Color = [ 0 0 0] EdgeColor = none EraseMode = normal Editing = off Extent = [0.493151 0.444444 0.260274 FontAngle = normal . FontName = Helvetica FontSize = [10] FontUnits = points FontWeight = normal 0.1]

HorizontalAlignment = left LineStyle = LineWidth = [0.5] Margin = [2] Position = [0.5 0.5 0] Rotation = [0] String = Hello, World! Units = data Interpreter = tex VerticalAlignment = middle BeingDeleted = off ButtonDownFcn = Children = [] Clipping = off CreateFcn = DeleteFcn = BusyAction = queue HandleVisibility = on HitTest = on Interruptible = on Parent = [102.007] Selected = off SelectionHighlight = on Tag = Type = text UIContextMenu = [] UserData = [] Visible = on

293

(, , z) . , text , (xi, yi, zi). , , , -, |. :text(x,,z,'string1)

294

7

text :text('XData',x,'YData',y,'ZData',z,'string','')

, , (. 7.4). 7.4 \alpha \beta \gamma \delta \epsilon \zeta \eta \theta \vartheta \iota \kappa \lambda \mu \nu \xi \pi \rho \sigma Warsigma \tau \equiv Mm \otimes i i ? u iP V a P Y 5 e \upsilon \phi \chi \psi \omega \Gamma Delta \Theta \Lambda C O 1) \sim \leq \infty \clubsuit \diamondsuit \heartsuit \spadesuit Meftrightarrow \leftarrow \uparrow \rightarrow \downarrow \circ \pm \geq \propto V 3 2 =

< oo * V A

\partial \bullet \div \neq \aleph \wp \oslash

X

Sie

295 7.4 ()

\ \supset \int \rfloor \lfloor \perp \wedge \rceil \vee Mangle

n J J L 1

\cup \subseteq \in \lceil \cdot \neg \times \surd \varpi \rangle

6

\supseteq \subset \ \nabla \ldots

V

-1 X

\prime \0 \rnid \copy right

' 0 I

1 V

V

>

. 7.19.

298

7

selected ( Selected=off), (. 7.20): text(, .5,'Hello, World!','Selected','on')

. . , .

| * Figure 1 %File Edit Ve insert Tools Desktop Wn o Help iw idw

-jalxj

&B4 . Q. S I^IQ 0 1 , * Q

0.8 0.64Hello. World!"

d.4 0.2) . - 0.2 - , 0 . 4 , 0.6 : 7:0.8

1

. 7.20.

7.7. Image image , , . , mxn . . , coata image. MATLAB .

.

299

RGB-, truecoior. CData (i, j) 1, : (i, j , l) ; (i, j , 2) ; (i, j , ) . , Windows, RGB- 8- 16- . (indexed) , CData mxn 255 65 535. (coiormap), . , , [, l], RGB-. BMP, 1, 4, 8 16 . . , CData (grayscale). CData [, l], Windows 8- 16- . , CData , truecoior. , image CDataMapping. direct, coiormap. CDataMapping scaled, . , image 3x4 ( 7.17).

300: -':

7

| 7.17. image X = [1 2 3 4; 5 6 7 8; 9 10 11 12] X= 1 2 3 4 5 6 7 8 9 10 11 12 h = image(X); colormap(colorcube(12)) %

% 3x4 %

colorcube (n) , . . 7.21.

5

1

15 :

2

25 .

3

35 .

4

45 .

. 7.21. 3x4 ( 7.18).

30?

get(h) AlphaData = [1] AlphaDataMapping = none CData = [ (3 by 3) double array] CDataMapping = direct EraseMode = normal XData = [1 4] YData = [1 3] BeingDeleted = off ButtonDownFcn = Children = [] Clipping = on CreateFcn = DeleteFcn = BusyAction = queue HandleVisibility = on HitTest = on Interruptible = on Parent = [101.001] Selected = off SelectionHighlight = on Tag = Type = image UIContextMenu = [] UserData = [] Visible = on

AlphaData AlphaDataMapping, , . CData , : get(h,'CData') 1 5 9 2 6 10 3 7 11 12 ans =

302

7

coataMapping , (CDataMapping=direct). EraseMode=normai , . set coata, . , , . EraseMode=none. XData YData . mxn XData=[i n] YData=[i m]. - , , 7.19. :

; 7.19. image(X,'XData',[-1 2],'YData',[2 4]);

colormap(colorcube(12))

, (. 7.22).

. 7.22.

303

, 12 , 3 . , . , , . , MALAB7\toolbox\aeroblks\aerodemos aeroblk_HL20pic.jpg. , \work aero.jpg. -, imfinfo ( 7.20).; imfinfo('aero.jpg') Filename: 'aero.jpg' FileModDate: '28-Mar-2002 21:40:54' FileSize: 17774 Format: 'jpg' FormatVef'sion: '' Width: 319 Height: 396 BitDepth: 24 ColorType: 'truecolor' FormatSignature: '' Comment: {}

ans =

, 319x396, 24 ( 8 , truecolor). image: A=imread('aero.j pg'); h_Img=image(A); , MATLAB . . 7.23 , .

304

7

File Edit View [nsert Tools Desktop Window Help

. 7.23.

: axis image

. axis image DataAspectRatio Axes [1 1 1]. (. 7.24) , . : m= 396 A=imread('aero.jpg'); [ n k]=size(A) m

319 k =

figure('Units ,'pixels','Position',[100 100 n m]) image (A); Figure 1 > File Edit View' Insert Tools Desktop Window' Help1

305

50

100

150

200

250

300

. 7.24.

. 7.25. , , : set(gca,'Position',[0 0 11])

, . 7.26.

,

, . imread . truecoior . RGB-: = imread('aero','jpg')

306

7

Figure 2 >flte |di Viev Inset Tool Desktc Windo 1[

File Edi Ve Inset Tool D s t Wn o Half * iv e kc i d

-infxj;

& # ' k! .e

8 m l

100

200

300

. 7 . 2 5 .

. 7 . 2 6 .

imread () ():>> [X,map]=imread('name', 'fmt') ;

, ('name'). : image(X);>> colormap(map)

coiormap(map) . , [, i]. RGB- , . coiormap . , Root. 64 -

307

, . 7.5. , , . 7.21, . 12 . 7.5

1 2 3 4 5 61

R0 0 0 0 0 0 0 0 0 0

G0 0 0 0 0 0 0 00,0625 0,1250 0,1875 0,2500 0,3125 0,3750 0,4375 0,5000 0,5625 0,6250 0,6875 0,7500 0,8125 0,8750 0,9375 1,0000 1,0000

0,5625 0,6250 0,6875 0,7500 0,8125 0,8750 0,9375 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 0,9375

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

R0,1250 0,1875 0,2500 0,3125 0,3750 0,4375 0,5000 0,5625 0,6250 0,6875 0,7500 0,8125 0,8750 0,9375 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000

G1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 0,9375 0,8750 0,8125 0,7500 0,6875 0,6250 0,5625 0,5000 0,4375 0,3750

0,8750 0,8125 0,7500 0,6875 0,6250 0,5625 0,5000 0,4375 0,3750 0,3125 0,2500 0,1875 0,1250 0,0625

8 9 11 12

10 0

13 0 14 0 15 0 16

0

0

17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0,0625

0 0 00 0 0 0 0

51 52 53 54 55 56 57

7 7.5 ()

R1,0000 1,0000 1,0000 1,0000 1,0000 1,0000 0,9375

G0,3125 0,2500 0,1875 0,1250 0,0625

0 0 0 0 0 0 0

58 59 60 61 62 63 64

0,8750 0,8125 0,7500 0,6875 0,6250 0,5625 0,5000

G0 0 0 0 0 0 0

0 0 0 0 0

0 0

0

image , : image(x,,)

: image(X,'XData',[-1 2],'YData',[2 4]);

image: h_Img=image('1',1,'2',2,...);

, imread, . 7.6. , (. . ), , , . imread . , imread. 7.6

'bmp'1

Windows Bitmap (BMP) Windows Cursor resources (CUR) Graphics Interchange Format (GIF) Hierarchical Data Format (HDF)

cur'

'gif' hdf'

309 7.6 ()

' ico' 'jpg' ' pbm' ' pcx' ' pgm' ' png' ' pnm' I 'jpeg'

Windows Icon resources (ICO) Joint Photographic Experts Group (JPEG) Portable Bitmap (PBM) Windows Paintbrush (PCX) Portable Graymap (PGM) Portable Network Graphics (PNG) Portable Anymap (PNM) : Portable Bitmap (PBM), Portable Graymap (PGM) Portable Pixel Map (PPM) Portable Pixmap (PPM) Sun Raster (RAS)

' ppm'1

ras ' I 'tiff

'tif ' xwd'

Tagged Image File Format (TIFF) X Windows Dump (XWD)

"" AiphaData AiphaDataMapping . ico imread 3 :[X,map,Alpha]=imread('matlab.ico');

, 32x32 . , , . , "", - , . Alpha, imread, , , . , ( i , j) , , . MATLAB , 7.21.! 7.21. size(X) ans = 32 32 ;

%

310 size(map) ans = 256 ans = 32 32 3 % "" size(Alpha)

7

%

: whos Name Alpha X map Size 32x32 32x32 256x3 Bytes Class 1024 1024 6144 logical array uint8 array double array

MATLAB , , , . Alpha (i, l ) : Alpha(1,1) 1 ans =

, . ,1)0 (1,:) 0 0 0 ans = ans =

, image - , , , : h_Img=image(X); colormap(map)

. 7.27. image . -, : m a p l = [map; 1 1 1 ] ;

311

1 5

20

;2 , , S

30

. 7.27.

-, , : XI = ones(size(X))*(length(mapl) - 1);

xi: XI(Alpha == 0) = X(Alpha == 0);

, image , xi double uints:>> image (uint8 (XI) ) ; colorma'p (mapl)

, . 7.28, , . , . AiphaData , CData. double, uints. AlphaDataMapping AiphaData : (AlphaDataMapping=none);11 . 899

312 (AlphaDataMapping=direct);

7

Alphamap, , , ALim (AlphaDataMapping=scaled).

1 0

1 > ,,' 2 5 , ..: 0

2 5

. 7.28.

imwrite, image , . imread, , :imwrite(A,'name','fmt') imwrite(X,map,'name', 'fmt') % colormap %

7.8. get set . , , , . , ?

313

( Figure), ( Axes) , (, Callback), gcf, gca gcbo . , findobj. - , . findobj , . , h_obj, findali ( 7.22). .............;.7..;,.!................. x=0:0.1:2*pi; plot (x, sin(x) ) h_All=findall(gca) h_All = 101.0020 3.0016

;

, . , , , ? ( 7.23)." ' " ; " " " ' * '

get(h_All(1),'Type') ans = axes get(h_All(2),'Type') ans = line

- findobj: hl=findobj('Type','axes') 101.0020 hi =

314 h2=findobj('Type','line') h2 = 3.0016

7

h2, get set. , f indob j : hl=findobj(gca,'Type','axes') hi = 101.0020 : findobj(gca) ans = 101.0020 3.0016 , findobj . , .

8

Windows- . , (Visual C++, Borland C++ Builder (), Delphi .), , MS-DOS. , . , . , Windows- , , , , . "" / , . , , , , , . . MATLAB Windows-, 2D- -. , MATLAB , . Windows. , . MATLAB . 8.1. Windows MATLAB Figure. , Axes, .

316

8 8.1

Push Button

Visual Basic (VB) CommandButton BCB, Delphi Button Visual C++Push Button

Toggle Button Radiobutton

,

BCB, Delphi SpeedButton VB Option Box BCB, Delphi RadioButton Visual C++ Radio Button

Checkbox

VB CheckBox BCB, Delphi CheckBox Visual C++ Check Box

Edit Text

,

VB TextBox BCB, D e l p h i - E d i t Visual C++ E d i t Box

Static Text

()

VB - Label BCB, Delphi - Label, StaticText Visual C++ Static Text

Slider

,

VB HScrollBar, VScrollBar BCB, Delphi ScrollBar Visual C++ Horizontal Vertical Scroll Bar

Panel

,

VB Frame BCB, Delphi Frame, Panel, Group Box Visual C++

Button Group

Radio Button Toggle Button

VB Frame BCB, Delphi Radio Group, Group Box Visual C++

317 8.1 ()

Listbox

,

VB ListBox , Delphi ListBox Visual C++ L i s t Box

Popup Menu

VB Menu E d i t o r BCB, Delphi PopMenu Visual C++ Menu

, . , MATLAB 41 (. 8.2). . , , ListBoxTop, , Listbox , . , slider, ( siiderstep). MATLAB . , Radio Button value 1, . . 8.2 BackgroundColor

( [0.753 0.753 0.753]) ( o f f ) ( queue)

, RGB- 0 1 on (), o f f () queue ( ), c a n c e l ()

BeingDeleted BusyAction

318

8

8.2 () ButtonDownFcn " " t r u e c o l o r , "" C a l l b a c k ( on) " " " " ( on) ( normal) normal ( ), i t a l i c (), o b l i c u e () on ( ), o f f ( ) on (), o f f ()

Cdata

Callback Children Clipping

CreateFcn DeleteFcn Enable Extent FontAngle

FontName FontSize

( MS , Windows Sans Serif)

() ( 8 ) ( points)inches (), centimeters (), normalized, pixels (), points (, 1 = 1/72 )

FontUnits

FontWeight

( normal)

normal (), light (), demi (), bold ()

319

8.2 () ForegroundColor Handle Visibility HitTest ( = [ 0 . 0 0.0 0.0]) ( on) ( on) ( center) ( on) " " , , ( 0) Value ( 1) Value ( 0) , , ( on) on (), o f f ( ) on (), o f f () RGB on (), o f f ( ) on ( ), o f f ( ) l e f t ( ), r i g h t ( ), c e n t e r ( ) on ( ), o f f ( )

Horizontal Alignment Interruptible

KeyPr&ssFcn

ListBoxTop

Max Min Parent Position

Selected SelectionHighlight

320

8 8.2 ()

SliderStep String

, , (, p u s h b u t t o n , s l i d e r .) , , , (, p u s h b u t t o n l , pushbutton2 .) ( ) , (, , . .). , ( ) , (, ) ( on)

Style

Tag

TooltipString Type UIContextMenu Units

inches (), centimeters (), normalized, p i x e l s (), p o i n t s ()

UserData

Value

Visible

on ( ), o f f ( )

7.0 Frame, Panel. , , . Frame , ( BorderType Borderwidth),

321

( TitlePosition). . 8.1 8.2 . etchedinetchedoutnone ; etchedin etchedout beveledin beveledout line BorderTypi . 8 . 1 . lefttop ; . ;:

-beveledin-

beveledout

line

centertop righttop leftbottom cenlerbottom rlghtbottom

TitlePosition . 8.2.

8.1. MATLAB . . , . uicontroi, :hUIC = uicontroi([hFig,] 'Style','_', 1 _1',_1,... ' 2', 2,... ' ', );

322

8

uicontrol , , () . set:set(UIC,'_1',_1,... '_2',_2,... '_',_)

a-x2 + b-x+c 0. , , ( 8.1). , y-a-x2 + b-x+cw . . (, , ) , roots, , poiyvai, . , short, , long, .\ 8.1. =1.5; =-2.4; =0.75; =[ b ]; x=roots() 1.1742 0.4258 p o l y v a K p , 1.1742) ;

X=

ans = 3.8460-005 polyvaKp, 0.4258) 3.8460-005 X = 1.17416573867739

323

ans = format long

x

0.42583426132261 polyvaKp, 1.17416573867739) ans = -4.329869796038111e-015 polyvaKp, 0.42583426132261) ans =-4.551914400963142e-015

/ Figure 1Pile Edit yi

Tools, gesktop Window _Help

. 8 . 3 . = - + -

324

8

, X xmin=min(real((1) ) , real(x(2)))-0.5 xmax=max(real(x(l)),real(x(2)))+0.5: xrl=real xmin=min(xrl,xr2)-0.5; xmax=max(xrl,xr2)+0.5; xx=xmin:0.1:xmax; plot (xx, yy) grid on xr2=real(x(2));

yy=polyval(p,xx);

. 8.3. , , . . , . ( ). , (. 8.4), roots2.ni ( 8.2).: 8.2. r o o t s 2 function roots2

% global hFig hAxes hBtn global hTxta hTxtb hTxtc hTxtxl hTxtx2 global hEda hEdb hEdc hEdxl hEdx2 h f i g = f i g u r e ( ' P o s i t i o n ' , [ 5 0 , 5 0 , 4 8 0 , 300]) ; hAxes=axes; set(hAxes,'Unit','pixels','Position',[30,70,280,220]) hTxta=uicontrol(hFig,'Style','text','String','a=', . . . 'Position', [340,253,30,21] , 'BackgroundColor1, [1 1 1] ', hTxtb=uicontrol(hFig,'Style','text', 'String','b=\ ... 'Position', [340,218,30,21],'BackgroundColor', [1 1 1] !

hTxtc=uicontrol(hFig,'Style','text','String','c=',... 'Position',[340,188,30,21],'BackgroundColor',[1 'Position',[340,113,30,21],'BackgroundColor',[1 hTxtx2=uicontrol(hFig,'Style','text','String','x2= 'Position', [340,78,30,21],'BackgroundColor',[1 1 1]); hEda=uicontrol(hFig,'Style','edit','Position',[380,250,100,25], . 'BackgroundColor',[1 1 1],'HorizontalAlignraent','left'); hEdb=uicontrol(hFig,'Style','edit','Position',[380,215,100,25],. 'BackgroundColor',[1 1 1],'HorizontalAlignment','left'); hEdc=uicontrol(hFig,'Style','edit','Position', [380,185,100,25] , . 'BackgroundColor',[1 1 1],'HorizontalAlignment','left'); hEdxl=uicontrol(hFig,'Style','edit','Position', [380,110,100,25], 'BackgroundColor', [1 1 1],'HorizontalAlignment','left'); hEdx2=uicontrol(hFig,'Style','edit','Position',[380,75,100,25],. 'BackgroundColor',[1 1 1],'HorizontalAlignment','left'); hBtn=uicontrol(hFig,'Style','pushbutton','String','',... 'Position',[340,150,140,25],'Callback','roots3'); [ Figure 1 File Edit View Insert Tools Desktop Window Help f > 0.6 . , , 0.2 ). b.2 0:4 ; 0.6 0.9;;

3251 1]); 1 1]);

hTxtxl=uicontrol(hFig,'Style','text','String','xl=',...

?!

,

... ;: ;; :|]

1= . " . x2= :. :: 1;

. 8.4.

roots2.m. , 35, : hFig ; hAxes ;

326

8

hBtn ; hTxta =; D hTxtb =; hTxtc =; hTxtxi xl=; hTxtx2 2=; hEda ; hEdb ; hEdc ; hEdxi xi; hEdx2 2; , . , , s t a t i c Text, , . figure, (50, 50), (480) (300). , Position. He , , . hFig . ( Axes) . axes . set units. normalized, (0, 0), (1, 1). ( [0.1 0.25 0.55 0.68]). , set. hAxes 4 . , . uicontroi, "" (hFig) -

327

( style). , , uicontroi- . . s t a t i c Text (, . . ) s t r i n g , . Push Button , . Edit Text ( ) string , l e f t HorizontaiAiignment . , ( BackgroundCoior). (callback-), roots3.m. 8.3. .

1. 2. 3. 4.

function roots3 global hAxes global hEda hEdb hEdc hEdxl hEdx2 axes(hAxes);

5. cla; 6. str=get(hEda,'String'); 7. a=str2num(str); 8. str=get(hEdb,'String'); 9. b=str2num(str); 10. str=get(hEdc,'String'); 11. c=str2num(str); 12. p=[a b c ] ; 13. x=roots(p) 14. xrl=real(x(l)); 15. xr2=real(x(2)); 16. xmin=min(xrl,xr2)-0.5; 17. xmax=max(xrl,xr2)+0.5; 18. xx=xmin: 0.1: xmax; 19. yy=polyval(p,xx); 20. plot(xx,yy) 21. grid on 22. set(hEdxl,'String',num2str(x(1))); 23. set(hEdx2,'String',num2str(x(2)));

328l E d i t o i - H;\MATU.L r. i ' u . , . . ]

8

File Edit Text Cell Tools Debug Desktop Window Help D at a i .* > get(hBtn) BackgroundColor = [0.831373 0.815686 0.784314] Callback = CData = [] Enable = on Extent =[0044] FontAngle = normal FontName = MS Sans Serif FontSize = [8] FontUnits = points FontWeight = normal ForegroundColor = [ 0 0 0] HorizontalAlignment = center KeyPressFcn = ListboxTop = [0] Max = [1] Min = [0] Position = [20 20 60 20] String = Style = pushbutton SliderStep = [0.01 0.1] TooltipString = Units = pixels Value = [0] BeingDeleted = off \

330ButtonDownFcn = Children = [] Clipping = on CreateFcn = DeleteFcn = BusyAction = queue HandleVisibility = on HitTest = on Interruptible = on Parent = [1] Selected = off SelectionHighlight = on Tag = Type = uicontrol UIContextMenu = [] UserData = [] Visible = on

8

( ) , . callback , ( Callback "" ). , (. 8.7).function pushbtn hfig=figuce('Position',[200,200,300,100]); hBtn=uiconttol('Style','pushbutton',... 'String',' 1'); >/ Figure 1 L-|D|X File Edit View Insert Tools Desktop Window Help

. 8 . 7 . ( S t r i n g = ' 1')

331

, , " ". , . , 80 (. 8.8).function pushbtn h f i g = f i g u c e ( ' P o s i t i o n ' , [200,200,300,100]) hBtnl=uicontrol('style','pushbutton',... 1 'String',' 1 ); hBtn2=uicontcol('Style','pushbutton',... 'String',' 2', . . . 'Position',[100,20,60,20]) ; File Edit View Insert Tools Desktop Window. Help:

. 8.8.

, (20, 20), 60 20 . . ( +). , callback. m-, . pushbtnl . , 1 1, 2 . 2 2 1. , , hBtnl hBtn2 callback . gcbo ( 8.5).

332;' . . .

8'-'-*"""""'

'

"

' .

'(/ ' . . '

; 8.5. pushbtn pushbtnlfunction pushbtn % , global hBtnl hBtn2 hfig=figure('Position',[200,200,300,100]); hBtnl=uicontrol('Style','pushbutton',... 'String',' 1',... 'Position',[10,20,70,25],... 'Callback','pushbtnl'); hBtn2=uicontrol ('Style', 'pushbutton',, . . . 'String',' 2',... 'Position', [100,20,70,25] 'Callback','pushbtnl'); end function pushbtnl % Callback global hBtnl hBtn2 set(hBtnl,'String',' 1') set(hBtn2,'String',' 21 ) if gcbo==hBtnl set(hBtnl,'String',' 11) end if gcbo==hBtn2 set(hBtn2,'String',' 2') end

!;

pushbtn , (. 8.9). ( j

C a l l b a c k m-, . . , . p u s h b t n l pushbtn.m. p u s h b t n end -

333

w a i t f o r (hf i g ) . w a i t f o r (hf ig) " " hf i g . pushbtn , pushbtni , . . h B t n l hBtn2. C a l l b a c k pushbtn . Figure 1 File Edit View Insert Tools Desktop Window,Help

. 8.9. ,

, , , TooltipString (. 8.10). function pushbtn global hBtnl hBtn2 hfig=figuce('Position', [200,200,300,120]); hBtnl=uicontcol('Style','pushbutton', ... 'StringT,' 1', . . . 'Position',[10,20,70,25], ... 'Callback ','pushbtni'); set(hBtnl,'TooltipString',' '); Figure 1 File Edit View Insert Tools Desktop: Window Help'- . ' ' ' ' / ' ':

' ' '

;

'

: : ;

1 I 2| [ | . 8.10.

, , MATLAB . Backgroundcoior

334

8

ForegroundColor ( 8.6). , , , RGB-.I hBtnl=uicontrol('Style','pushbutton',... 'String',' 1',... 'Callback','pushbtnl'); set(hBtnl,'BackgroundColor',[0 1 0],... 'ForegroundColor', 'Red');

. . callback , . , ButtonDownFcn, : , 5- ; , . ButtonDownFcn - , , callback. , Callback , Enable off ( ) inactive ( ). ButtonDownFcn , , , . ( drag & drop). , , . , "" createFcn. , . DeieteFcn .

335

8.1.2. , Toggle Button , . , , . , . ( Zoom () ). Paint . . | (Edit | Paste), 24- bmp ( zoom.bmp). tbutton . , imread CData ( 8.7). . 8.11 , .

" 8.7.function tbutton hFig=figure('Position',[50,50,300,100]>; hTBtnl=uicontrol('Style','togglebutton',... 'Position',[50,50,30,30]); a=imread ( zoom, bmp', ' bmp') ; ' set(hTBtnl,'CData',a); hTBtn2=uicontrol('Style','togglebutton',... 'Position ,[82,50,30,30]); set(hTBtn2,'CData', a ) ;1

, , value. , . vaiue=Min , .

336

8

File Edit .View Insert Tools. Desktop Window: Help

. 8 . 1 1 .

8.1.3. , panel Button Group -, (Radiobutton) (checkbox) . , . , . -, , . , . . 8.3.

File Edit View Insert Tools Desktop Window Help

. 8.12.

337

, hAxes. , . 8.12. , , units. Units=normalized, . , , , (. . set ). buttongroupl.m callback , 8.8.

function buttongroupl global hAxes global hRbl h 2 hRb3 hFig=figure('Position', [50,50,500,300]); hAxes=axes; set(hAxes,'Units','pixels','Position',[50,50,350,230]); hRbl=uicontrol('Style','radiobutton',... 'String','sin(x)',... 'Position',[420,250,60,20]); hRb2=uicontrol('Style','radiobutton',... 'String','cos(x)',... 'Position',[420,220,60,20]); hRb3=uicontrol('Style','radiobutton',... 'String','exp(x)',... 'Position',[420,190,60,20]);

, buttongroupl () . callback, radiol.m ( 8.9).

338 8.9. function radiol % Callback global hAxes global hRbl hRb2 hRb3 axes(hAxes) cla x=0:0.1:6.28; if gcbo==hRbl set(hRb2,'Value',0) set(hRb3,'Value',0) y=sin(x); plot (x,y) ; end if gcbo==hRb2 s