Introduction to MATLAB 3Scripts, Functions,real .ppt

上传人:wealthynice100 文档编号:376681 上传时间:2018-10-08 格式:PPT 页数:34 大小:578.64KB
下载 相关 举报
Introduction to MATLAB 3Scripts, Functions,real .ppt_第1页
第1页 / 共34页
Introduction to MATLAB 3Scripts, Functions,real .ppt_第2页
第2页 / 共34页
Introduction to MATLAB 3Scripts, Functions,real .ppt_第3页
第3页 / 共34页
Introduction to MATLAB 3Scripts, Functions,real .ppt_第4页
第4页 / 共34页
Introduction to MATLAB 3Scripts, Functions,real .ppt_第5页
第5页 / 共34页
亲,该文档总共34页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、Introduction to MATLAB 3 Scripts, Functions,real programming,Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment i.brookssee.leeds.ac.uk,Scripts,Any set of Matlab commands saved to a (text) file with .m file extension Whole script can be run simply by entering the fi

2、lename (without .m) on the command line Executes in the interactive workspace exactly as if you entered each line by line Script can access existing variables Any variables created by script remain in workspace when it has finished running,figure orient landscape wysiwygclear all load /worker/twelve

3、/coastal_waves/jun07/data/section_jun07_3sub1=subplot(2,2,1); section_plot(gtheta,x,y,x,nonew); extcontour(x(:,1),y,grhum,label,color,w,fontsize,9); txt=findobj(sub1,type,text); set(txt,color,w) xlimit=get(gca,xlim); ylimit=get(gca,ylim); aa=text(xlimit(1),ylimit(2)+0.05*diff(ylimit),a); set(aa,font

4、size,11,fontweight,bold,color,k);sub2=subplot(2,2,2); section_plot(gmws,x,y,0 30,x,nonew); xx,yy,gu=reducegrid(x(:,1),y,gxuic,2,2); xx,yy,gv=reducegrid(x(:,1),y,gxvic,2,2); windvec(xx,yy,gu,gv,screen,nonew,w); xlimit=get(gca,xlim); ylimit=get(gca,ylim); bb=text(xlimit(1),ylimit(2)+0.05*diff(ylimit),

5、b); set(bb,fontsize,11,fontweight,bold);,Example script: bams_fig8.m,Functions,A MATLAB function is very similar to a script, but: Starts with a function declaration line function out1,out2=function_name(in1) May have defined input arguments May have defined output arguments Executes in its own work

6、space: it CANNOT see, or modify variables in the calling workspace values must be passed as input & output variables,function s,c,t = allTrig(x_deg) % input angle in degrees % output sin, cos, tanx_rad = 2*pi*x_deg./360; s = sin(x_rad); c = cos(x_rad); t = tan(x_rad);,Structure of a Function M-file,

7、Output Argument(s),Input Argument(s),Online Help,There are many functions built-in or supplied with MATLAB. A few very basic functions are built in to the matlab executable Very many more are supplied as m-files; the code can be viewed in command window by entering: type function_nameor in the edito

8、r by entering edit function_name Supplied m-files can be found, grouped by category, in $matlab_root_dir/toolbox/catagory,Workspaces or Stacks in MATLAB,MATLAB (or base) workspace: For command line variables. Function workspaces: Each function has its own workspace for local variables. Communicate t

9、o function workspace via inputs and outputs. (Promotes structured coding and prevents name conflicts.) Global workspace: Global variables can be shared by multiple workspaces. (Must be initialized in all relevant workspaces.)NOTE: Script files share the workspace of the function they are called from

10、, or the base workspace if called from the command line.,Function Help,MATLAB uses the % character to start a comment everything on line after % is ignored A contiguous block of comment lines following the first comment, is treated as the help text for the function. This block is echoed to screen wh

11、en you enter help function_name This is very usefuluse it when writing code!, help epotMATLAB FUNCTION EPOT Calculates theta_e by Boltons formula (Monthly Weather Review 1980 vol.108, 1046-1053)usage: epot=epot(ta,dp,pr)outputsepot : equivalent potential temperature (K) inputs ta : air temp (K)dp :

12、dew point (K)pr : static pressure (mb)IM Brooks : july22 1994,e.g. help text from a function to calculate equivalent potential temperature,function epot=epot(ta,dp,pr) % MATLAB FUNCTION EPOT % Calculates theta_e by Boltons formula (Monthly Weather % Review 1980 vol.108, 1046-1053) % % usage: epot=ep

13、ot(ta,dp,pr) % % outputs % epot : equivalent potential temperature (K) % inputs ta : air temp (K) % dp : dew point (K) % pr : static pressure (mb) % % IM Brooks : july22 1994% ensure temperatures are in kelvin ta(tatemp, set dewpoint equal to temp dp(dpta)=ta(dpta);% calculate water vapour pressure

14、and mass mixing ratio mr=mmr(dp,pr); vap=vp(dp);% calculate temperature at the lifing condensation level TL=(2840./(3.5*log(ta) - log(vap) - 4.805)+55;% calculate theta_e epot=ta.*(1000./pr).(0.2854*(1-0.28E-3*mr).* .exp(3.376./TL - 0.00254).*mr.*(1+0.81E-3*mr);,What it does,How its called,What inpu

15、t and outputs are (and units!),Good coding practice, note what and whyyou will forget,Editing code,Can use ANY ascii text editor Windows: Notepad Linux: vi, emacs, k-edit, kate,Matlab has a built in editor, to start it: editor to edit an existing code edit filename.m,Matlab editor will: Color-code t

16、ext according to context/syntax Highlight incorrect syntax (errors) Highlight points where code could be more efficient,Line numbers (given by error messages),Code optimisation flag,Code state: green: perfect red: errors orange: hints,Words highlighted blue are reserved words,The parts of a function

17、,Function declaration line (header) defines input & output variables Assignment statements assign values to variables, modify values of existing variable Output statements display outputs: graphics, numeric values, write files Programme flow-control decision making (logic), loop control, iteration,F

18、unction declaration,out1,out2=afunction(in1,in2,in3,in4) it is not necessary to use ALL of the output arguments (variables) when calling the function. It is possible to write functions to handle variable numbers of inputs & outputs e.g. use of optional inputs, or changing behaviour in response to nu

19、mber of outputs called.You CANNOT use more inputs/outputs than defined in the function declaration.,Input variables,output variables,Handling optional input/output variables,nargin returns number of input arguments used in actual function call nargout returns number of output arguments used in actua

20、l function call,function x,y=myfunction(a,b,c) % help text if nargin3 c=1; end rest of code,Set a default value if an input variable is not supplied,If, else, elseif,if conditionstatements; elseif conditionstatements; elsestatements; end,Generic form:,if A0disp(A is positive) elsedisp(A is neither)

21、end,example,condition a logical statement: evaluates to true (1) or false (0) statements and other valid matlab statements,disp function displays a string in command window,if conditionstatements; elsestatements; endif conditionstatements; end,Dont need all the parts of if/then/else,for loops,for n

22、= firstn:dn:lastnstatements; end,Generic form:,for n = 1:10x(n)=n2; end,example,N.B. loops are rather inefficient in MATLAB, the example above would execute much faster if vectorized as x=1:10.2; If you can vectorize code instead of using a loop, do so.,N.B. can specify non-integer values for nbut c

23、an only use integers as array indices,while loops,while conditionstatements; end,Generic form:,n = 1; while n = 10x(n)=n2;n=n+1; end,example,The statements within the while loop are executed repeatedly while the condition is true. Example does exactly the same as the for loop in previous example (an

24、other inefficient loop). If the condition is false when first tested, the statements in the while loop will never be executed.,Initialise control variable before starting loop,Modify control variable within loop,while loops are often used where it is not known in advance how many iterations of the l

25、oop will be required,done = false; count = 0; while donecount = count + 1;lasta = a2;a2 = fix(B2-B1)/(PWR*da)*da;if abs(a2-lasta)(3*da)done=1;end end,The values of lasta and a2 change each time round the loop, iteration continues until the condition in if statement is true,Indent code within blocks

26、i.e. Between pairs of: if.end if.else.elseif.end while.end forendIndent one level for EACH nested block of code. Indenting makes it MUCH easier to read your code, and thus easier to debug.,Keeping code readable,Matlab stores arrays as contiguous blocks of memory. If an array grows inside a loop e.g.

27、 for n=1:5000X(n) = n.2; endThen matlab may have to keep moving the location of the variable in memory repeatedly copying it this slows down the code. If you know how big the variable needs to be, predefine it to pre-allocate the required block of memory.,A = 1:500000; x = zeros(size(A); for n = 1:l

28、ength(A)x(n) = somefunction(A(n); end,Pre-allocating arrays,Debugging code,If you have an error in your code, and error message will be displayed when it is run, e.g.? Error: File: quadplot.m Line: 14 Column: 23 Unexpected MATLAB expression.This (usually) gives a hint as to the type of error, and th

29、e location in the code. NB. Since matlab code is not compiled (in traditional sense) errors are identified ONLY when that line of code is run. If you cant figure out the problem just by examining the code,.use interactive mode inside a function,Add temporary statements to code to display information

30、 you need as it runs: Use the keyboard function This halts execution of the function and returns command to the keyboard/command window, but leaves you in the functions workspace can inspect variables, change values, etc. Enter return to resume running the function(N.B. if used inside a loop, contro

31、l will be passed to keyboard on every iteration),Setting breakpoints,Matlab editor has a debug menu Set breakpoints to stop code at a particular point and allow you to step through line by line Set conditional breakpoints that stop execution if a certain condition is met Set stop-on-error flag code

32、will stop and pass control to keyboard if an error or warning is encountered(warnings are non-fatal errors, code will still run but is technically invalid, e.g. divide by zero),ctrl-c,If code is running much longer than seems reasonable it may be stuck in an infinite loop Condition for exiting a whi

33、le loop never met Condition being tested in wrong place Control variable ina for loop getting reset so its maximum value is never reachedCan force code to stop with ctrl-c,Non-fatal errors,Just because your code doesnt have any syntax errors, and runs OK, doesnt mean it does what you intended.If the

34、 results you get dont make sense, you probably have an error in the logic underlying your codewelcome to life as a programmer!,Strings,MATLAB treats strings as arrays of characters A 2D string matrix must have the same number of characters on each row!, name = Ian,Brooks name =IanBrooks name=Ian;Bro

35、oks ? Error using = vertcat All rows in the bracketed expression must have the same number of columns name=Ian ;Brooks Name =IanBrooks,Can concatenate strings as:, firstname = Ian; secondname = Brooks; fullname = firstname, ,secondname fullname =Ian Brooks,Strings defined within single quotation mar

36、ks quotation mark within a string needs double quoting (2 single quotes), thetitle = Ian Brookss matlab class thetitle =Ian Brookss matlab class,String functions,strcmp(str1,str2) - returns true (1) if str1 & str2 are equal findstr(S1,S2) - returns index to location of any occurrences of shorter str

37、ing in longer strfind(text,pattern) - returns index to occurrences of pattern in text See also strncmp, strmatch,helpdesk string functions section,Break the job down into discrete parts write the code to do each part For complex tasks start with a flow chart or algorithm COMMENT you code Use meaning

38、ful variable names they can be as long as neededIf part a section of code needs to run more than oncethink about making it a separate function or sub-function,Good code management Keep code in one or more dedicated directories separate from data Use meaningful function names Include a help section o

39、f comments at the start of each function include details of inputs/outputs and any optionsBeg/borrow/steal code from colleaguesdont waste time reinventing something thats already been done. Almost all programming is changing existing code, not writing new stuff! (honest!),Good programming style,Save

40、 groups of related functions in their own directoryBy applicationBy projectMatlab will only find code that is: Part of core matlab installation In the current working directory On the search pathCan add directories to the path with the addpath command, e.g.: addpath d:matlabmeteorology,startup.m fil

41、e: if this file exists on the path, it will be run automatically at startup:Windows network install: M:matlab directory should be created when matlab first run. Put startup.m in hereLINUX /home/USERNAME/matlab,Helping you stay organised,%- % Matlab startup file for IMBs laptop %-%- add paths for my

42、m-files addpath d:/matlab/aerosol addpath d:/matlab/coastal addpath d:/matlab/lidar addpath d:/matlab/ndbc addpath d:/matlab/page addpath d:/matlab/sections addpath d:/matlab/sharem addpath d:/matlab/sonics addpath d:/matlab/wavelet addpath d:/matlab/LEM addpath d:/matlab/GPSbook addpath d:/matlab/F

43、AAM addpath d:/matlab/FAAM/winds addpath d:/matlab/faam/bae addpath d:/matlab/TREX addpath d:/matlab/SEASAW addpath d:/matlab/SEASAW/turbulence addpath d:/matlab/SEASAW/SBWR addpath d:/matlab/tethersonde addpath d:/matlab/chilbolton addpath d:/matlab/ASCOS addpath d:/matlab/ASCOS/scratch addpath d:/

44、matlab/sodar,% COARE bulk flux algorithm addpath d:/matlab/bulkflux/cor2.5 addpath d:/matlab/bulkflux/cor2.6 addpath d:/matlab/bulkflux/cor3.0 addpath d:/matlab/bulkflux/gasflux/CO2 addpath d:/matlab/bulkflux/gasflux/ozone% netCDF Tools addpath d:/matlab/MEXCDF % my netCDF functions addpath d:/matla

45、b/MEXCDF/mexnc addpath d:/matlab/MEXCDF/mexnc/tests addpath d:/matlab/MEXCDF/snctools addpath d:/matlab/MEXCDF/snctools/tests%- add path for generic data - addpath d:/matlab/coastlines % coastline data%- %- set default figure options set(0,DefaultFigurePaperUnits,inches) % v6 defaults to cm for EU countries set(0,DefaultFigureRenderer,painters) % v7 default OpenGL causes problems,Put any code you ALWAYS want run into startup.m Example startup.m file (from my laptop),

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 教学课件 > 大学教育

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1