matlab读取excel数据

上传人:公**** 文档编号:513888516 上传时间:2022-11-20 格式:DOC 页数:7 大小:41.50KB
返回 下载 相关 举报
matlab读取excel数据_第1页
第1页 / 共7页
matlab读取excel数据_第2页
第2页 / 共7页
matlab读取excel数据_第3页
第3页 / 共7页
matlab读取excel数据_第4页
第4页 / 共7页
matlab读取excel数据_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《matlab读取excel数据》由会员分享,可在线阅读,更多相关《matlab读取excel数据(7页珍藏版)》请在金锄头文库上搜索。

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

2、接查看matlab自带的帮助。 xlsread对于纯数据的xls文件支持很完美,也就是说当xls文件里的每个格子都是“数”时,xlsread会直接返回一个实数矩阵。但是通 常我们拿到xls文件并不是这样,它的表头多半是描述性文字,它的数据也有可能是文字,有些位置的数据还有可能是缺失的。xlsread对这样的文件读取 无能为力,或者说需要大量的时间去协调数据的位置信息。要是有一个函数,能够按照原有的顺序直接读取所有的单位格数据就好了。当然,这时候返回的矩阵就不 能是一个数值矩阵了,它将会是一个cell矩阵,里面的每个元素类型可能不一样。 matlab本身并不提供这个功能,但是另外有一个函数off

3、icedoc完美的实现这个功能。这个函数包可以去OfficeDoc官方网站上去下载,解压缩后放到工作路径上即可。使用方法可以查询help officedoc。officedoc是收费函数包,但有免费版本,而且其免费版本可以实现上面我们所说的效果(收费版本主要是可以用来修改office文件)。 例子: 在matlab中读取xls格式的文件内容如应用如下函数: 1.bb=xlsread(c:feature.xls,a0:an40),其中:c:feature.xls为文件存放的地址,a0:a40为将要读取的单元格的范围.bb为读取的矩阵在MATLAB中的变量名. 2.使用m文件脚本如下: Excel

4、 = actxserver(Excel.Application); set(Excel, Visible, 1); Workbooks = Excel.Workbooks; Workbook = invoke(Workbooks, Open, cd,featureABC.xls); %读取 ABC.xls:sheet1 a1(即 R1C1)an40(即 R240c40) 范围内的40by40矩阵 read_excel=ddeinit(excel,ABC.xls:sheet1); feature1 = ddereq(read_excel, R1c1:R40c40); feature1 % 关闭A

5、BC.xls invoke(Excel, Quit); delete(Excel); 注意:在使用时将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) reads the dat

6、a 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,TXT,RAW=XLSREAD(FILE,SHEET,RANGE) reads the data specified in RANGE from the worksheet SHEET, in the

7、 Excel file specified in FILE. It is possible to select the range of data interactively (see Examples below). Please note that the full functionality 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 abo

8、ve, using basic input mode. This is the mode used on UNIX platforms 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, the wh

9、ole 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,CUSTOMFUN) When the Excel COM server is used, allows passing in a handle to a cus

10、tom function. This function will be called just before retrieving the actual data from Excel. It must take an Excel Range object (e.g. of 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,

11、 which will be returned from XLSREAD as the fourth output argument, 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. Default extension is xls.

12、 SHEET: string defining worksheet name in workbook FILE. double scalar defining worksheet index in workbook FILE. See 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

13、available (e.g. on Unix). 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 and TXT are subsets of RAW. EXAMPLES: 1. Default operation: NUMERIC = xls

14、read(FILE); NUMERIC,TXT=xlsread(FILE); NUMERIC,TXT,RAW=xlsread(FILE); 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(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 the first sheet: NUMERIC = xlsread(c:m

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 小学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号