matlab读取excel数据_第1页
matlab读取excel数据_第2页
matlab读取excel数据_第3页
matlab读取excel数据_第4页
matlab读取excel数据_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、matlab读取excel       office的表格文件也就是xls文件本质上就是一个二维矩阵,二维矩阵是用来保存数据的最佳方式,所以在日常工作中,我们从其它地方获取的数据通常都被保存为xls格式,但处理数据时,我们却需要把xls文件的数据导入到matlab里进行处理。       如果你只处理一个文件并且只做一次的话,你可以手动来拷贝粘贴,这花费不了你太多时间。如果有很多xls文件,或者你的xls文件的内容可能随时被修改,那么下面的方法可以派上用场。    

2、   matlab自身提供了大量的函数,包括读取office文件。其中xlsread和xlswrite就是专门用来读取xls文件里的数据的。这两个函数的使用方法可以直接查看matlab自带的帮助。       xlsread对于纯数据的xls文件支持很完美,也就是说当xls文件里的每个格子都是“数”时,xlsread会直接返回一个实数矩阵。但是通 常我们拿到xls文件并不是这样,它的表头多半是描述性文字,它的数据也有可能是文字,有些位置的数据还有可能是缺失的。xlsread对这样的文件读取 无能为力,或者说需要大量的时间去协

3、调数据的位置信息。要是有一个函数,能够按照原有的顺序直接读取所有的单位格数据就好了。当然,这时候返回的矩阵就不 能是一个数值矩阵了,它将会是一个cell矩阵,里面的每个元素类型可能不一样。        matlab本身并不提供这个功能,但是另外有一个函数officedoc完美的实现这个功能。这个函数包可以去OfficeDoc官方网站上去下载,解压缩后放到工作路径上即可。使用方法可以查询help officedoc。officedoc是收费函数包,但有免费版本,而且其免费版本可以实现上面我们所说的效果(收费版本主要是可以用来修改off

4、ice文件)。       例子:       在matlab中读取xls格式的文件内容如应用如下函数:       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) 范围内的 40by40 矩阵         read_excel=ddeinit('excel','ABC.xls:sheet1');   &

7、#160;     feature1 = ddereq(read_excel, 'R1c1:R40c40');         feature1 %  关闭ABC.xls         invoke(Excel, 'Quit');         delete(Excel);      

8、;     注意:在使用时将m文件与xls文件存于同一个目录下.另外:sheet1:可以重命名,且读取sheet的名称要和实际存放的名称相同. matlab读取excel,txt文件函数注意matlab不识别中文,读写的文件中最好不含有中文excel读取函数 xlsreadtext  读取函数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 表格数据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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论