《matlab讲义10.ppt》由会员分享,可在线阅读,更多相关《matlab讲义10.ppt(22页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Saving WorkspaceDirectory ManagementFormatted OutputLow-Level I/OImporting ExcelWeek 10FunctionDescriptionsavesave filenamesave filename var1 var2.Save workspace to matlab.mat.Save to filename.mat.Save only variables var1 var2.loadload filenameload filename var1 var2.Load workspace from matlab.mat.L
2、oad from filename.mat.Load only variables var1 var2.Saving Workspace Workspace space of all variablesSave workspace no need to recreate them next timeLoad workspace re-load what you have savedand only want to store B for later use.Use:a=rand(20,1);B=toeplitz(a);Suppose you have generated:save lastse
3、ssion BWhen we want to use the matrix B again in the future,we can just reload them:load lastsessionWorkspace WindowYou can also save the workspace by using“File”then“Save Workspace as.”,or highlighting the variables and right-clicking them.If you cant find your workspace window or other windows(e.g
4、.current directory window),go to the command window,click“View”and check the window you want.FunctionDescriptioncd directorya=cd(directory.name)Make directory the current directory.It results in a string variable a.delete filename Delete the file filenamedir directorydirList the files in directory.L
5、ist the files in the current directory.pwda=pwd Show current directory.Current directory is stored in a.Directory ManagementCurrent directory is the one Matlab looks in first to get files.cdDisplays the current directory.C:MATLAB6p5work cd.Go up one level.pwd Show current directoryans=C:MATLAB6p5 cd
6、 work Go back down to the subdirectory.pwd ans=C:MATLAB6p5work dir Display all files in current directory.GenData.m WindTable.m temp.jpg.Q1.dat Q2.dat temp.dat dir*.dat Display files with.dat extension.Q1.dat Q2.dat temp.dat dir Q*.dat Display.dat files starting with Q.Q1.dat Q2.datShow current dire
7、ctory and files in the directory.Current Directory WindowAnother way of changing directories is to use the menu bar.pathMATLABPATHC:MATLAB6p5toolboxmatlabgeneralC:MATLAB6p5toolboxmatlabopsC:MATLAB6p5toolboxmatlablangC:MATLAB6p5toolboxmatlabelmatetcSearch PathPath is a list of directories in which Ma
8、tlab looks for M-files and data.The current directory is in it,plus a lot of directories used by Matlab.You can examine this list by using the path command:FunctionDescriptionpath Show current paths.addpath(directory)Prepend directory to the current paths.rmpath(directory)Remove directory from the c
9、urrent paths.If your M-file or function is not in the path,Matlab will not be able to find it.You then have to move the file to a directory on the path,or simply add the directory onto the path.Managing the PathSuppose my function files are in C:homeRaymond.To access the files,we can:addpath(C:homeR
10、aymond)pathMATLABPATHC:homeRaymondC:MATLAB6p5toolboxmatlabgeneralC:MATLAB6p5toolboxmatlabopsC:MATLAB6p5toolboxmatlablangC:MATLAB6p5toolboxmatlabelmat.How to automatically change the current directory,set up required paths,etc,when Matlab first starts?Just write the commands in startup.m and put it i
11、n matlabroottoolboxlocal,where matlabroot is where the Matlab program is.An example of startup.m.Startup Defaultscd C:homeRaymondaddpath(C:homestudents)display(hello)diaryFunctionDescriptionsprintf(format,a1,.)Create strings a1,.according to formatfprintf(format,a1,.)Like sprintf,but printed in Comm
12、and WindowFormatted Outputsprintf controls the printing of numbers in different formats.The first argument is the format string which specifies the appearance of how the numbers a1,are printed.Format%m.nLDescription%md Integer.Blanks added to the left,total number of characters is m.%m.nf Fixed poin
13、t,with n decimal.Blanks added to the left,total number of space is m.%m.ne Like f with a signed 3-digit exponent(e.g.e+000).Total character count m includes five from the exponent.%m.ng Chooses f or e,whichever is shorter%mc Picks off a single character from a string.Blanks added to the left,total n
14、umber of characters is m.%ms Prints a string.Blanks added to the left,total number of characters is m.CommandResultsprintf(%1d,round(pi)3 1 charactersprintf(%3d,round(pi)3 2 blanks on the leftsprintf(%5.3f,pi)3.142 3 decimal pointssprintf(%8.3f,pi)3.142 3 decimal points with 3 blanks sprintf(%10.3e,
15、pi)3.142e+000 10 characters with 3 dec.ptssprintf(%12.3e,pi)3.142e+000 sprintf(%5.3g,pi)3.14 sprintf(%10.3g,pi*1e-4)0.000314 sprintf(%10.3g,pi*1e-6)3.14e-006 sprintf(%4c,hi)hisprintf(%4s,hi)hiFunctionDescriptionfid=fopen(file)fid=fopen(file,permission)Open file for read only and return the file iden
16、tifier fid.permission specifier can be:r read,w write,a append,r+read and writefprintf(fid,format,a1,.)Write variables a1,.according to format to the file identified by fid.fscanf(fid,format)Read data using format from the file identified by fid.fclose(fid)fclose(all)Close the file with identifier f
17、id.Close all files.Low-level file I/O Sometimes we may want to write data to a file for human to read or read data created by human.%Conversion table between Fahrenheit and Celsius%open file table.txt for writing and call it file1file1=fopen(table.txt,w);%print heading.n means a new linefprintf(file
18、1,Fahrenheit Celsiusn);for i=0:300 fprintf(file1,%11d,i);fprintf(file1,%11.4fn,(i-32)*5/9);end%close the file table.txtfclose(file1);Fahrenheit Celsius 0 -17.77778 1 -17.22222 2 -16.66667 3 -16.11111 4 -15.55556 5 -15.00000 6 -14.44444 7 -13.88889 8 -13.33333 9 -12.77778 10 -12.22222 .Results in tab
19、le.txt:FunctionDescriptiona,b=xlsread(filename)Read in Excel file,assigning data to a and labels to b.Data in Microsoft Excel spreadsheet can be imported into Matlab by the xlsread command.Importing Excel Filesxlsread reads in spreadsheet,assigns the data to the a and the labels to b.Blank cells are
20、 assigned NaN.One may then extract the data by array indexing(e.g.A=a(j:k,n:m).Consider a spreadsheet called WindChill.xls:a,b=xlsread(WindChill)a=NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 101520253035404550NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 51713192531364248NaN 10-439152127344046NaN 15-706131925323845NaN 20-9-24111724303744NaN 25-11-4391623293643NaN 30-12-5181522283542NaN 35-14-7071421283541NaN 40-15-8-161320273441b=Wind(mph)Temperature(deg F)