




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、matlab讀取excel office的表格文件也就是xls文件本質(zhì)上就是一個二維矩陣,二維矩陣是用來保存數(shù)據(jù)的最佳方式,所以在日常工作中,我們從其它地方獲取的數(shù)據(jù)通常都被保存為xls格式,但處理數(shù)據(jù)時,我們卻需要把xls文件的數(shù)據(jù)導(dǎo)入到matlab里進(jìn)行處理。 如果你只處理一個文件并且只做一次的話,你可以手動來拷貝粘貼,這花費(fèi)不了你太多時間。如果有很多xls文件,或者你的xls文件的內(nèi)容可能隨時被修改,那么下面的方法可以派上用場。
2、 matlab自身提供了大量的函數(shù),包括讀取office文件。其中xlsread和xlswrite就是專門用來讀取xls文件里的數(shù)據(jù)的。這兩個函數(shù)的使用方法可以直接查看matlab自帶的幫助。 xlsread對于純數(shù)據(jù)的xls文件支持很完美,也就是說當(dāng)xls文件里的每個格子都是“數(shù)”時,xlsread會直接返回一個實(shí)數(shù)矩陣。但是通 常我們拿到xls文件并不是這樣,它的表頭多半是描述性文字,它的數(shù)據(jù)也有可能是文字,有些位置的數(shù)據(jù)還有可能是缺失的。xlsread對這樣的文件讀取 無能為力,或者說需要大量的時間去協(xié)
3、調(diào)數(shù)據(jù)的位置信息。要是有一個函數(shù),能夠按照原有的順序直接讀取所有的單位格數(shù)據(jù)就好了。當(dāng)然,這時候返回的矩陣就不 能是一個數(shù)值矩陣了,它將會是一個cell矩陣,里面的每個元素類型可能不一樣。 matlab本身并不提供這個功能,但是另外有一個函數(shù)officedoc完美的實(shí)現(xiàn)這個功能。這個函數(shù)包可以去OfficeDoc官方網(wǎng)站上去下載,解壓縮后放到工作路徑上即可。使用方法可以查詢help officedoc。officedoc是收費(fèi)函數(shù)包,但有免費(fèi)版本,而且其免費(fèi)版本可以實(shí)現(xiàn)上面我們所說的效果(收費(fèi)版本主要是可以用來修改off
4、ice文件)。 例子: 在matlab中讀取xls格式的文件內(nèi)容如應(yīng)用如下函數(shù): 1.bb=xlsread('c:feature.xls','a0:an40'),其中:c:feature.xls為文件存放的地址,a0:a40為將要讀取的單元格的范圍.bb為讀取的矩陣在MATLAB中的變量名. 2.使用m文件腳
5、本如下: Excel = actxserver('Excel.Application'); set(Excel, 'Visible', 1); Workbooks = Excel.Workbooks;
6、 Workbook = invoke(Workbooks, 'Open', cd,'featureABC.xls'); % 讀取 ABC.xls:sheet1 a1(即 R1C1)an40(即 R240c40) 范圍內(nèi)的 40by40 矩陣 read_excel=ddeinit('excel','ABC.xls:sheet1'); &
7、#160; feature1 = ddereq(read_excel, 'R1c1:R40c40'); feature1 % 關(guān)閉ABC.xls invoke(Excel, 'Quit'); delete(Excel);
8、; 注意:在使用時將m文件與xls文件存于同一個目錄下.另外:sheet1:可以重命名,且讀取sheet的名稱要和實(shí)際存放的名稱相同. matlab讀取excel,txt文件函數(shù)注意matlab不識別中文,讀寫的文件中最好不含有中文excel讀取函數(shù) xlsreadtext 讀取函數(shù)csvreadXLSREAD Get data and text from a spreadsheet in an Excel workbook. NUMERIC,TXT,RAW=XLSREAD(FILE) r
9、eads the data specified in the Excel file, FILE. The numeric cells in FILE are returned in NUMERIC, the text cells in FILE are returned in TXT, while the raw, unprocessed cell content is returned in RAW. NUMERIC,
10、TXT,RAW=XLSREAD(FILE,SHEET,RANGE) reads the data specified in RANGE from the worksheet SHEET, in the Excel file specified in FILE. It is possible to select the range of data interactively (see Examples below). Please note that the full functiona
11、lity of XLSREAD depends on the ability to start Excel as a COM server from MATLAB. NUMERIC,TXT,RAW=XLSREAD(FILE,SHEET,RANGE,'basic') reads an XLS file as above, using basic input mode. This is the mode used on UNIX platforms
12、 as well as on Windows when Excel is not available as a COM server. In this mode, XLSREAD does not use Excel as a COM server, which limits import ability. Without Excel as a COM server, RANGE will be ignored and, consequently,
13、the whole active range of a sheet will be imported. Also, in basic mode, SHEET is case-sensitive and must be a string. NUMERIC,TXT,RAW=XLSREAD(FILE,SHEET,RANGE,'',CUSTOMFUN) NUMERIC,TXT,RAW,CUSTOMOUTPUT=XLSREAD(FILE,SHEET,RANGE,
14、9;',CUSTOMFUN) When the Excel COM server is used, allows passing in a handle to a custom function. This function will be called just before retrieving the actual data from Excel. It must take an Excel Range object (e.g. of &
15、#160; type 'Interface.Microsoft_Excel_5.0_Object_Library.Range') as input, and return one as output. Optionally, this custom function may return a second output argument, which will be returned from XLSREAD as the fourth output argu
16、ment, CUSTOMOUTPUT. For details of what is possible using the EXCEL COM interface, please refer to Microsoft documentation. INPUT PARAMETERS: FILE: string defining the file to read from. Default directory is pwd.
17、60; Default extension is 'xls'. SHEET: string defining worksheet name in workbook FILE. double scalar defining worksheet index in workbook FILE. See
18、 NOTE 1. RANGE: string defining the data range in a worksheet. See NOTE 2. MODE: string enforcing basic import mode. Valid value = 'basic'. This is the mode always used when COM is not available (e.g. on Unix
19、). RETURN PARAMETERS: NUMERIC = n x m array of type double. TXT = r x s cell string array containing text cells in RANGE. RAW = v x w cell array containing unprocessed numeric and text data. Both NUMERIC
20、and TXT are subsets of RAW. EXAMPLES: 1. Default operation: NUMERIC = xlsread(FILE); NUMERIC,TXT=xlsread(FILE); NUMERIC,TXT,RAW=xlsread(FILE);&
21、#160; 2. Get data from the default region: NUMERIC = xlsread('c:matlabworkmyspreadsheet') 3. Get data from the used area in a sheet other than the first sheet: NUMERIC = xlsread(
22、'c:matlabworkmyspreadsheet','sheet2') 4. Get data from a named sheet: NUMERIC = xlsread('c:matlabworkmyspreadsheet','NBData') 5. Get data from a specified region in a sheet other than
23、the first sheet: NUMERIC = xlsread('c:matlabworkmyspreadsheet','sheet2','a2:j5') 6. Get data from a specified region in a named sheet: NUMERIC
24、= xlsread('c:matlabworkmyspreadsheet','NBData','a2:j5') 7. Get data from a region in a sheet specified by index: NUMERIC = xlsread('c:matlabworkmyspreadsheet',2,'a2:j5') 8. I
25、nteractive region selection: NUMERIC = xlsread('c:matlabworkmyspreadsheet',-1); You have to select the active region and the active sheet in the EXCEL window that will come into focus.
26、 Click OK in the Data Selection Dialog when you have finished selecting the active region. 9. Using the custom function: NUMERIC,TXT,RAW,CUSTOMOUTPUT = xlsread('equity.xls', ., MyCustomFun)
27、 Where the CustomFun is defined as: function DataRange, customOutput = MyCustomFun(DataRange) DataRange.NumberFormat = 'Date'
28、; customOutput = 'Anything I want' This will convert to dates all cells where that is possible. NOTE 1: The first worksheet of the workbook is the default sheet. If
29、 SHEET is -1, Excel comes to the foreground to enable interactive selection (optional). In interactive mode, a dialogue will prompt you to click the OK button i
30、n that dialogue to continue in MATLAB. (Only supported when Excel COM server is available.) NOTE 2: The regular form is: 'D2:F3' to select rectangular region D2:F3 &
31、#160; in a worksheet. RANGE is not case sensitive and uses Excel A1 notation (see Excel Help). (Only supported when Excel COM server is available.) NOTE 3: Excel formats ot
32、her than the default can also be read. (Only supported when Excel COM server is available.) See also xlswrite, csvread, csvwrite, dlmread, dlmwrite, textscan. Reference page in Help browser
33、60; doc xlsread CSVREAD Reada comma separated value file. M = CSVREAD('FILENAME') reads a comma separated value formatted file FILENAME. The result is returned in M. The file can only contain
34、 numeric values. M = CSVREAD('FILENAME',R,C) reads data from the comma separated value formatted file starting at row R and column C. R and C are zero- based so that R=0 and C=0 specifies the first value in the file.
35、0; M = CSVREAD('FILENAME',R,C,RNG) reads only the range specified by RNG = R1 C1 R2 C2 where (R1,C1) is the upper-left corner of the data to be read and (R2,C2) is the lower-right corner. RNG can also be specified using
36、spreadsheet notation as in RNG = 'A1.B7'. CSVREAD fills empty delimited fields with zero. Data files where the lines end with a comma will produce a result with an extra last column filled with zeros.
37、See also csvwrite, dlmread, dlmwrite, load, fileformats, textscan. Reference page in Help browser doc csvreadMatlab如何讀取Excel 表格數(shù)據(jù)Subject: Are there any examples that show how to use the ActiveX automation interface to connect MATLAB to Excel? Pr
38、oblem Description I am trying to control Excel from MATLAB using ActiveX. Are there any examples that show how to use the ActiveX automation interface from Excel to do this? Solution: Most of the functionality that you get from ActiveX is dependent on the object model, which the external application
39、 implements. Consequently, we are usually unable tp provide much information about the functions that you need to use in the remote application to perform a particular function. We do, however, have an example that shows how to do perform common functions in Excel. We also recommend that you become
40、more familiar with the Excel object model in order to better use Excel's ActiveX automation interface from MATLAB. You can find more information on this interface by selecting the "Microsoft Excel Visual Basic Reference" topic in the Microsoft Excel Help Topic dialog. This topic area c
41、ontains a searchable description of Excel methods and properties. The following example demonstrates how to insert MATLAB data into Excel. It also shows how to extract some data from Excel into MATLAB. For more information, refer to the individual comments for each code segment. % Open Excel, add wo
42、rkbook, change active worksheet, % get/put array, save, and close % First open an Excel Server Excel = actxserver('Excel.Application'); set(Excel, 'Visible', 1); % Insert a new workbook Workbooks = Excel.Workbooks; Workbook = invoke(Workbooks, 'Add'); % Make the second sheet
43、active Sheets = Excel.ActiveWorkBook.Sheets; sheet2 = get(Sheets, 'Item', 2); invoke(sheet2, 'Activate'); % Get a handle to the active sheet Activesheet = Excel.Activesheet; % Put a MATLAB array into Excel A = 1 2; 3 4; ActivesheetRange = get(Activesheet,'Range','A1:B2
44、9;); set(ActivesheetRange, 'Value', A); % Get back a range. It will be a cell array, % since the cell range can % contain different types of data. Range = get(Activesheet, 'Range', 'A1:B2'); B = Range.value; % Convert to a double matrix. The cell array must contain only scalars. B = re
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 5年級下冊英語書單詞表點(diǎn)讀
- 低空空中交通應(yīng)用場景
- 登山 法治宣傳活動
- 4年級觀察日記三則怎么寫
- 超聲波塑料焊接 - 副本 - 副本
- 2025年貴陽幼兒師范高等??茖W(xué)校單招職業(yè)技能測試題庫帶答案
- 2025年云南商務(wù)職業(yè)學(xué)院單招職業(yè)傾向性測試題庫一套
- 2025年重慶市綿陽市單招職業(yè)傾向性測試題庫及參考答案
- 2025年天津公安警官職業(yè)學(xué)院單招職業(yè)技能測試題庫1套
- 2025年晉城職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫學(xué)生專用
- 綠色金融與ESG分析
- 2024年家電市場發(fā)展趨勢及2025年消費(fèi)趨勢分析報(bào)告-GfK
- 2024年陜西省初中學(xué)業(yè)水平考試·數(shù)學(xué)
- 勞榮枝案件分析報(bào)告
- 火電廠汽機(jī)車間安全培訓(xùn)
- 社區(qū)網(wǎng)格員消防安全培訓(xùn)
- 剪刀式登高車安全技術(shù)交底
- 部編人教版小學(xué)4四年級《道德與法治》下冊全冊教案
- 新疆2022年中考數(shù)學(xué)試卷(含答案)
- LED顯示屏培訓(xùn)資料
- 2024年監(jiān)理考試-公路工程監(jiān)理工程師考試近5年真題附答案
評論
0/150
提交評論