Introduction to MATLAB 3Scripts, Functions,real .ppt
《Introduction to MATLAB 3Scripts, Functions,real .ppt》由会员分享,可在线阅读,更多相关《Introduction to MATLAB 3Scripts, Functions,real .ppt(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
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- INTRODUCTIONTOMATLAB3SCRIPTS FUNCTIONS REALPPT

链接地址:http://www.mydoc123.com/p-376681.html