數(shù)學(xué)軟件課件文件操作_第1頁
數(shù)學(xué)軟件課件文件操作_第2頁
數(shù)學(xué)軟件課件文件操作_第3頁
數(shù)學(xué)軟件課件文件操作_第4頁
數(shù)學(xué)軟件課件文件操作_第5頁
已閱讀5頁,還剩40頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、數(shù)學(xué)軟件Matlab篇Mathematical Software for Section Matlab第四講 文件操作Lecture four: File orpration In Matlab environment, There are two type files as following: 1. Program file or M-file General program and function program. 2. Data file The system not only provide general function of file management, but also

2、 gives a lot of special function for file operation. MATLAB 環(huán)境下的文件與其它系統(tǒng)一樣,也有兩類文件組成,一類是程序文件,又稱M文件,另一類是數(shù)據(jù)文件。系統(tǒng)除提供了文件的一般管理功能外,還提供了對數(shù)據(jù)文件進行操作的特殊功能函數(shù)。4.1 基本命令 (Basic command)1. Help command HELP Display help text in Command Window. Form: help Example: help plot help matlabgeneral2. What command WHAT List

3、Matlab-specific files in directory. Form: what Example: what matlab List all the M-files of matlab directory.3. Type commamd TYPE List M-file. type foo.bar %lists the ascii file called foo.bar. type foo %lists the ascii file called foo.m. Form: type 4. Lookfor command Form: lookfor 尋找命令或字符串是否存在。 Exa

4、mple: lookfor cos4.1 基本命令 (Basic command)5which command of function search which item displays the full path for item. 顯示item所在的全路徑。 str = which(item) returns the full path for item in the string, str. 將item的全路徑返回到變量str。 Example: which pinv, str = which(pinv);6 path command of control path PATH view

5、 or change search path. 查看或改變搜索路徑。 path displays the Matlab search path, which is stored in pathdef.m. 顯示Matlab搜索路徑,儲存在pathdef.m中。 path(newpath) changes the search path to newpath, where newpath is a string array of folders. 將當前搜索路徑設(shè)置為newpath。4.1 基本命令 (Basic command)6 path command of control path pa

6、th(path,newpath) adds the newpath folder to the end of the search path. If newpath is already on the search path, then path(path, newpath) moves newpath to the end of the search path. path(newpath,path) adds the newpath folder to the top of the search path. If newpath is already on the search path,

7、then path(newpath, path) moves newpath to the top of the search path. To add multiple folders in one statement, instead use addpath. p = path returns the search path to string variable p. Example: path(path, c:toolsgoodstuff) 4.1 基本命令 (Basic command)7who, whos 顯示變量命令 WHO list current variables. (Onl

8、y name) WHOS list current variables as a long form of WHO. (Including Name, Size, Bytes and Class)8 load, save 裝載,保存workspace命令 load filename or load(filename) loads all variables from the MAT-file, filename into workspace. load filename variables or load(filename,variables) loads the specified vari

9、ables from the MAT-file, filename. load filename -ascii or load(filename,-ascii) treats filename as an ASCII file, regardless of the file extension.4.1 基本命令 (Basic command) save filename or save(filename) saves all variables from the current workspace in a Matlab formatted binary file (MAT-file) cal

10、led filename. If filename already exists, save overwrites the file. save test or save(test) or save(test.mat) Saves all variables of workspace to file test.mat. save test x y Only save x, y to file test.mat. save pqfile.txt x y -ascii or save(pqfile.txt,x,y,-ascii) Uses 8-digit ascii form instead of

11、 binary regardless of file extension. save . -ascii double Uses 16-digit ASCII form. save . append Adds the variables to an existing file (MAT-file only).4.1 基本命令 (Basic command)9 clear 清除變量命令 clear removes all variables from the current workspace, releasing them from system memory.Example: clear %

12、Removes all variables from the workspace. clear x y % Clear x, y clear a* % Clear variables starting with a10 disp 顯示文本或變量內(nèi)容 disp(X) displays the array, without printing the array name. If X is a string, the text is displayed. Example: x=1 2 3 disp(x) y=I love you. disp(y)4.1 基本命令 (Basic command)11

13、cd 改變目錄命令 CD, by itself, prints out the current directory. CD directory-spec sets the current directory to directory-spe. CD . moves to the directory above the current one. WD = CD returns the current directory as a string.12 dir 顯示目錄內(nèi)容命令 DIR list directory. 顯示目錄里的文件。Example: dir C:Program Files cd(

14、C:Program Files) D=dir(C:Program Files) 4.1 基本命令 (Basic command)13Delete command Delete command deletes file or graphics object. 刪除文件或?qū)ο竺睢?delete file_name deletes the named file from disk.Form: delete %可以使用通配符*,? delete Example1: H=plot(X, X) % 建立圖形對象H delete(H) % 刪除圖形對象HExample2: delete *.p % del

15、etes all P-files from the current directory. 4.1 基本命令 (Basic command)4.2 文件的打開與關(guān)閉Matlab提供了對數(shù)據(jù)文件建立、打開、讀、寫以及關(guān)閉等一系列函數(shù),數(shù)據(jù)文件一般存放在磁盤等介質(zhì)上,用文件名標識,系統(tǒng)對文件名沒有特殊要求。文件數(shù)據(jù)格式有二種形式,一是二進制格式文件,二是ASCII文本文件,系統(tǒng)對這兩類文件提供了不同的讀寫功能函數(shù)。4.2 高級文件的輸入輸出 File import/export functions.dlmread - Read delimited text file.dlmwrite - Write

16、 delimited text file.load - Load workspace from MATLAB (MAT) file.importdata - Load workspace variables disk file.wk1read - Read spreadsheet (WK1) file.wk1write - Write spreadsheet (WK1) file.xlsread - Read spreadsheet (XLS) file. 4.2 高級文件的輸入輸出 File import/export functions.dlmread - Read delimited t

17、ext file.dlmwrite - Write delimited text file.load - Load workspace from MATLAB (MAT) file.importdata - Load workspace variables disk file.wk1read - Read spreadsheet (WK1) file.wk1write - Write spreadsheet (WK1) file.xlsread - Read spreadsheet (XLS) file. 4.2 高級文件的輸入輸出 File import/export functions.d

18、lmread - Read delimited text file.dlmwrite - Write delimited text file.load - Load workspace from MATLAB (MAT) file.importdata - Load workspace variables disk file.wk1read - Read spreadsheet (WK1) file.wk1write - Write spreadsheet (WK1) file.xlsread - Read spreadsheet (XLS) file. DLMWRITE 寫ASCII分隔的文

19、件 dlmwrite(filename, M, DLM) 將矩陣 M寫入文件filename,用DLM作為分隔符。指定 t產(chǎn)生tab-分隔(4個空格)的文件。 dlmwrite(filename, M, DLM, R, C) 從文件filename中偏移R行,C列開始將矩陣 M寫入,用DLM作為分隔符。 R和C都基于零(0)的,所以 R=C=0指文件中第一數(shù)據(jù)。4.2 高級文件的輸入輸出 DLMREAD 讀ASCII分隔的文件 result= dlmread(filename, delimiter) 從分隔符為delimiter的文件filename中讀取數(shù)值數(shù)據(jù),結(jié)果由 RESULT返回.用

20、 t表示tab. result = dlmread(filename, delimiter, R, C) 從delimiter分隔的文件filename中讀取數(shù)據(jù)。 R 和C指明文件中數(shù)據(jù)的左上角 R行和 C列。 R和C都基于零(0)的,所以 R=0和C=0指文件中第一數(shù)據(jù)。 result = dlmread(filename, delimiter, range) 在由range = R1 C1 R2 C2指定的范圍內(nèi)讀,其中 (R1,C1) 為要讀的數(shù)據(jù)的左上角, (R2,C2)為右下角。range 也可以用表單符號(spreadsheet notation )指定,例如range = A1

21、.B7。 dlmread用零填充空分隔域。用非空白分隔符作為行結(jié)束的數(shù)據(jù)文件,將產(chǎn)生帶零填充的額外的最后列的結(jié)果 。4.2 高級文件的輸入輸出 XLSREAD 讀 Excel電子表格 (XLS)文件 A = xlsread(filename) 返回Microsoft Excel文件filename中的第一表單中的數(shù)值數(shù)據(jù) A, B = xlsread(filename) 數(shù)值數(shù)據(jù)存入A中,文本數(shù)據(jù)存入 B. . = xlsread(filename,sheetname) a,b=xlsread(c:book1.xls,程述漢) 與上相同,但用表單名sheetname. 若表單sheetname

22、不存在,則為一錯誤。 空單元格(Empty cells) ,或具有文本的單元格在數(shù)值結(jié)果中產(chǎn)生NaN。 文本打頭行(Leading rows and columns of text)不在數(shù)值結(jié)果中產(chǎn)生NaN。 這使得裝載這樣的文件很方便。4.2 高級文件的輸入輸出 XLSREAD 讀 Excel電子表格 (XLS)文件 為從文件中獲得有用的表單名,調(diào)用具有兩個輸出的xlsfino函數(shù),如: status, sheetnames = xlsfinfo(filename) 當從Excel文件中讀取日期域時,日期數(shù)據(jù)必須轉(zhuǎn)換為MATLAB日期。如,若在Excel文件中包含: 4/12/99 4/13

23、/99 4/14/99 象這樣把數(shù)據(jù)轉(zhuǎn)換為 MATLAB日期: excelDates = xlsread(filename) matlabDates = datenum(30-Dec-1899) + excelDates4.2 高級文件的輸入輸出 4.3 文件的打開與關(guān)閉. Fopen function FOPEN open file. 打開文件。 FID = fopen(Filename) opens the file Filename for read access. (On PC systems, fopen opens files for binary read access.) FI

24、D is a scalar MATLAB integer, called a file identifier. If FOPEN cannot open the file, it returns -1. FID= fopen(Filename, Permission) opens the file Filename in the mode specified by Permission. FID, MESSAGE = fopen(Filename, Permission) returns a system dependent error message if the open is not s

25、uccessful.4.3 文件的打開與關(guān)閉The Permission can be:r readw write (create if necessary)a append (create if necessary)r+ read and write (do not create)w+ truncate or create for read and write 為讀/寫建立一個新的文本文件a+ read and append (create if necessary)W write without automatic flushing寫但不自動覆蓋A append without autom

26、atic flushing 追加數(shù)據(jù)但不自動覆蓋4.3 文件的打開與關(guān)閉Such as, to open the data file std.dat for reading, we can use following command: FID=fopen(std.dat, r)Files can be opened in binary mode (the default) or in text mode. To open in text mode, add t to the permission string, for example rt and wt+.打開文件的默認格式為二進制格式,如果

27、想用ASCII文本格式,須在格式字符串中加上字符t,例如用rt表示以ASCII格式打開供讀操作的數(shù)據(jù)文件。4.3 文件的打開與關(guān)閉2 fclose關(guān)閉文件 ST = fclose(FID) closes the file with file identifier FID, which is an integer obtained from an earlier FOPEN. FCLOSE returns 0 if successful and -1 if not. 文件在進行完讀、寫等操作后,應(yīng)及時關(guān)閉,以保證文件的安全可靠。關(guān)閉文件命令格式為: ST=fclose(FID) %關(guān)閉FID所表

28、示的文件其中ST表示關(guān)閉文件操作的返回代碼,若關(guān)閉成功,返回0,否則返回1。 二進制數(shù)據(jù)文件 A = fread(FID) reads binary data from the specified file and writes it into matrix A. FID is an integer file identifier obtained from fopen. A = fread(FID, sizeA) reads the number of elements specified by SIZE. Valid entries for sizeA are: N read N elem

29、ents into a column vector.inf read to the end of the file.M,N read elements to fill an M-by-N matrix, in column order. N can be inf, but M cant. A = fread(FID,sizeA,precision) reads the file according to the data format specified by the string precision. A,COUNT=fread() optional output argument COUN

30、T returns the number of elements successfully read.4.4 文件的讀寫操作 Any of the following strings, either the MATLAB version, or their C or Fortran equivalent, may be used. If not specified, the default precision is uchar. MATLAB C or Fortran Description uchar unsigned char unsigned character, 8 bits. sch

31、ar signed char signed character, 8 bits. int8 integer*1 integer, 8 bits. int16 integer*2 integer, 16 bits. int32 integer*4 integer, 32 bits. int64 integer*8 integer, 64 bits. uint8 integer*1 unsigned integer, 8 bits.無符號整數(shù) uint16 integer*2 unsigned integer, 16 bits. uint32 integer*4 unsigned integer,

32、 32 bits. uint64 integer*8 unsigned integer, 64 bits. single real*4 floating point, 32 bits. float32 real*4 floating point, 32 bits. double real*8 floating point, 64 bits. float64 real*8 floating point, 64 bits.4.4 文件的讀寫操作short, long, ushort, ulong, integer*4(1,2,4,8), real*4(4,8) 等也是有效的precision。Ex

33、ample: FID=fopen(std.dat, r); A=fread(FID, 100, long); Sta=fclose(FID);以讀數(shù)據(jù)方式打開數(shù)據(jù)文件std.dat,并按長整型數(shù)據(jù)格式讀取文件的前100個數(shù)據(jù)放入向量A,然后關(guān)閉文件。4.4 文件的讀寫操作 Fwrite function write binary data to file. COUNT = fwrite(FID,A,Precision) writes the elements of matrix A to the specified file, translating MATLAB values to the

34、specified precision. The data are written in column order. COUNT is the number of elements successfully written. Precision controls the form and size of the result. See the list of allowed precisions under Fread.Example: FID=fopen(magic5.bin, w+); fwrite(FID, magic,int32);4.4 文件的讀寫操作【例1】建立一數(shù)據(jù)文件test.

35、dat,用于存放矩陣A的數(shù)據(jù)。已知A=-0.6515 -0.2727 -0.4354 -0.3190 -0.9047 -0.7534 -0.4567 -0.3212 -0.4132 -0.3583 -0.9264 -0.8173 -0.7823 -0.3265 -0.0631 -0.1735 -0.7373 -0.0972 -0.3267 -0.6298 -0.4768 -0.6773 -0.6574 -0.1923 -0.43894.4 文件的讀寫操作 下述程序段將矩陣A的數(shù)據(jù)以二進制浮點數(shù)格式寫入文件test.dat中。 Fid=fopen(test.dat, w) cnt=fwrite(

36、Fid, A, float) fclose(Fid) 下述程序讀取文件test.dat的內(nèi)容。 Fid=fopen(test.dat, r) B,cnt=fread(Fid,5,inf, float) fclose(Fid)4.4 文件的讀寫操作2. 文本文件 Fscanf read formatted data from file. 讀ASCII文本文件。 A = fscanf(fileID,formatSpec) reads data from an open text file into column vector A and interprets values in the file

37、according to the format specified by formatSpec. The fscanf function reapplies the format throughout the entire file and positions the file pointer at the end-of-file marker. If fscanf cannot match formatSpec to the data, it reads only the portion that matches and stops processing. 按formatSpec指定的格式讀

38、取文本文件fileID的全部數(shù)據(jù)到列向量A,數(shù)據(jù)和解釋價值的文件按指定格式的formatspec。fscanf函數(shù)重復(fù)的格式,使文件指針指向文件結(jié)尾標記。如果數(shù)據(jù)與formatspec不匹配, fscanf只讀取部分匹配的并停止處理。4.4 文件的讀寫操作2. 文本文件 formatSpec is a string containing C language conversion specifications. Conversion specifications involve the character %, optional assignment-suppressing asterisk

39、and width field, and conversion characters d, i, o, u, x, e, f, g, s, c, and . Example: formatSpec is %*d %s4.4 文件的讀寫操作2. 文本文件 formatSpec用來控制讀取的數(shù)據(jù)格式,由% 加上格式符組成,常用格式如下: %d -十進制有符號整數(shù) %5d讀一個多達 5 位的有符號整數(shù)或直到下一分隔符 %u -十進制無符號整數(shù)(類似于%d) %o -八進制無符號整數(shù) %x -十六進制無符號整數(shù) %f - Floating-point fields can contain any of

40、 the following (not case sensitive): Inf, -Inf, NaN, or -NaN. %5f - reads up to 5 digits or until next delimiter %e -浮點數(shù)(科學(xué)計數(shù)法) %g 浮點數(shù)(自動)4.4 文件的讀寫操作2. 文本文件%s - Read a string until fscanf encounters white space %5s - reads up to 5 characters or until white space%c - Read any single character, includ

41、ing white space. To read multiple characters at a time, specify field width. %5c - reads up to 5 characters including whitespace%. - Read only characters in the brackets up to the first nonmatching character or white space.Example: %mus reads summer as summ. %i Signed integer, base 10 or base 8 or b

42、ase 16, according to the values in the file 4.4 文件的讀寫操作2. 文本文件 A = fscanf(fileID,formatSpec,sizeA) reads file data into an array, A, with dimensions, sizeA, and positions the file pointer after the last value read. fscanf populates A in column order. sizeA=n讀取n個數(shù)據(jù)到一個列向量;sizeA=n,m讀取mn個數(shù)據(jù)到一個mn矩陣中,按列存放

43、;sizeA=INF讀取整個文件。 A,count = fscanf(_) additionally returns the number of fields that fscanf reads into A. For numeric data, this is the number of values read. You can use this syntax with any of the input arguments of the previous syntaxes. Count為成功讀取到A中的數(shù)據(jù)個數(shù)。4.4 文件的讀寫操作Examples: S = fscanf(FID,%s)

44、reads (and returns) a character string. A = fscanf(FID,%5d) reads 5-digit decimal integers. b= fscanf(FID, %6.2f) reads 6-digit decimal floating point with 2-digit decimal fraction.4.4 文件的讀寫操作4.4 文件的讀寫操作fprintf 函數(shù)寫ASCII數(shù)據(jù)文件,其格式為: nbytes= fprintf(FID,formatSpec, A,)其中A為要寫入文件的數(shù)據(jù)矩陣,先按formatSpec格式化數(shù)據(jù)矩陣A

45、,后寫入到Fid所指定的文件。nbytes為寫入數(shù)量。 fprintf(FID,formatSpec,A1,.,An) 寫入多個矩陣。Example: x = 0: 0.1: 1; y = x; exp(x); Fid = fopen(exp.txt, w); fprintf(Fid,%6.2f %12.8fn,y); fclose(Fid);fgetl 和 fgets fgetl和fgets命令都是用來讀取文件的下一行,兩者的差別是fgetl會舍去換行符,而fgets則保留換行符。語法:tline=fgetl(FID) %讀取文件的下一行,不包括換行符tline=fgets(FID) %讀取

46、文件的下一行,包括換行符tline=fgets(FID,nchar) %限制讀取文件字符個數(shù)說明: FID為文件標識號(ID);tline為以字符串形式的返回值,如果到文件末尾則返回-1;nchar為最多返回的字符個數(shù)。4.4 文件的讀寫操作【例】以行的形式讀取Ex01.txt文件。fid=fopen(Ex01.txt,r) ; %打開Ex01.txt文件只讀 fgetl(fid) %讀取第一行數(shù)據(jù) fgets(fid) %讀取第二行數(shù)據(jù) fgets(fid,10) %讀取第三行數(shù)據(jù),限制10個字符 fgets(fid,10) %繼續(xù)讀取第三行數(shù)據(jù),限制10個字符fgets(fid) %讀取第

47、三行剩余的數(shù)據(jù) fgets(fid) %讀取第四行數(shù)據(jù) fclose(fid) ) %關(guān)閉Ex01.txt4.4 文件的讀寫操作4.5 數(shù)據(jù)文件定位寫操作1. fseek函數(shù) Fseek set file position indicator. STATUS = fseek(FID, OFFSET, ORIGIN) repositions the file position indicator in the file with the given FID to the byte with the specified OFFSET relative to ORIGIN. OFFSET value

48、s are interpreted as follows: 0 Move toward the end of the file. = 0 Do not change position. 0 Move toward the beginning of the file. ORIGIN values are interpreted as follows: bof or -1 Beginning of file cof or 0 Current position in file eof or 1 End of file STATUS is 0 on success and -1 on failure.4.5 數(shù)據(jù)文件定位寫操作1. fseek函數(shù)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論