matlab读取excel

合集下载

matlab读取excel数据

matlab读取excel数据

matlab读取exceloffice的表格文件也就是xls文件本质上就是一个二维矩阵,二维矩阵是用来保存数据的最佳方式,所以在日常工作中,我们从其它地方获取的数据通常都被保存为xls格式,但处理数据时,我们却需要把xls文件的数据导入到matlab里进行处理。

如果你只处理一个文件并且只做一次的话,你可以手动来拷贝粘贴,这花费不了你太多时间。

如果有很多xls文件,或者你的xls文件的内容可能随时被修改,那么下面的方法可以派上用场。

matlab自身提供了大量的函数,包括读取office文件。

其中xlsread和xlswrite就是专门用来读取xls文件里的数据的。

这两个函数的使用方法可以直接查看matlab自带的帮助。

xlsread对于纯数据的xls文件支持很完美,也就是说当xls文件里的每个格子都是“数”时,xlsread会直接返回一个实数矩阵。

但是通常我们拿到xls文件并不是这样,它的表头多半是描述性文字,它的数据也有可能是文字,有些位置的数据还有可能是缺失的。

xlsread 对这样的文件读取无能为力,或者说需要大量的时间去协调数据的位置信息。

要是有一个函数,能够按照原有的顺序直接读取所有的单位格数据就好了。

当然,这时候返回的矩阵就不能是一个数值矩阵了,它将会是一个cell矩阵,里面的每个元素类型可能不一样。

matlab本身并不提供这个功能,但是另外有一个函数officedoc完美的实现这个功能。

这个函数包可以去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 = actxserver('Excel.Application');set(Excel, 'Visible', 1);Workbooks = Excel.Workbooks;Workbook = invoke(Workbooks, 'Open', [cd,'\feature\ABC.xls']);%% 读取ABC.xls:sheet1 a1(即R1C1)~an40(即R240c40) 范围内的 40by40 矩阵read_excel=ddeinit('excel','ABC.xls:sheet1');feature1 = ddereq(read_excel, 'R1c1:R40c40');feature1%% 关闭ABC.xlsinvoke(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 data specified in the Excelfile, FILE. The numeric cells in FILE are returned in NUMERIC, the textcells in FILE are returned in TXT, while the raw, unprocessed cellcontent is returned in RAW.[NUMERIC,TXT,RAW]=XLSREAD(FILE,SHEET,RANGE) reads the data specifiedin RANGE from the worksheet SHEET, in the Excel file specified in FILE.It is possible to select the range of data interactively (see Examplesbelow). Please note that the full functionality of XLSREAD depends onthe ability to start Excel as a COM server from MATLAB.[NUMERIC,TXT,RAW]=XLSREAD(FILE,SHEET,RANGE,'basic') reads an XLS file asabove, using basic input mode. This is the mode used on UNIX platformsas 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 limitsimport ability. Without Excel as a COM server, RANGE will be ignoredand, consequently, 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,'',CUSTOMFUN) When the Excel COM server is used, allows passing in a handle to acustom function. This function will be called just before retrievingthe actual data from Excel. It must take an Excel Range object (e.g. oftype 'Interface.Microsoft_Excel_5.0_Object_Library.Range') as input,and return one as output. Optionally, this custom function may returna second output argument, which will be returned from XLSREAD as thefourth output argument, CUSTOMOUTPUT. For details of what is possibleusing 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'.SHEET: string defining worksheet name in workbook FILE.double scalar defining worksheet index in workbook FILE. SeeNOTE 1.RANGE: string defining the data range in a worksheet. See NOTE 2.MODE: string enforcing basic import mode. Valid value = 'basic'. Thisis the mode always used when COM is not 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 = xlsread(FILE);[NUMERIC,TXT]=xlsread(FILE);[NUMERIC,TXT,RAW]=xlsread(FILE);2. Get data from the default region:NUMERIC = xlsread('c:\matlab\work\myspreadsheet')3. Get data from the used area in a sheet other than the first sheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2')4. Get data from a named sheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData')5. Get data from a specified region in a sheet other than the firstsheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2','a2:j5')6. Get data from a specified region in a named sheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData','a2:j5')7. Get data from a region in a sheet specified by index:NUMERIC = xlsread('c:\matlab\work\myspreadsheet',2,'a2:j5')8. Interactive region selection:NUMERIC = xlsread('c:\matlab\work\myspreadsheet',-1);You have to select the active region and the active sheet in theEXCEL window that will come into focus. Click OK in the DataSelection Dialog when you have finished selecting the active region.9. Using the custom function:[NUMERIC,TXT,RAW,CUSTOMOUTPUT] = xlsread('equity.xls', ..., @MyCustomFun) Where the CustomFun is defined as:function [DataRange, customOutput] = MyCustomFun(DataRange)DataRange.NumberFormat = 'Date';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 SHEET is -1, Excel comes to the foreground to enable interactiveselection (optional). In interactive mode, a dialogue will promptyou to click the OK button in 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 in a worksheet. RANGE is not case sensitive and uses Excel A1notation (see Excel Help). (Only supported when Excel COM serveris available.)NOTE 3: Excel formats other 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 browserdoc xlsreadCSVREAD 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 containnumeric 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.M = CSVREAD('FILENAME',R,C,RNG) reads only the range specifiedby RNG = [R1 C1 R2 C2] where (R1,C1) is the upper-left corner ofthe data to be read and (R2,C2) is the lower-right corner. RNGcan also be specified using spreadsheet notation as in RNG = 'A1..B7'.CSVREAD fills empty delimited fields with zero. Data files wherethe lines end with a comma will produce a result with an extra lastcolumn filled with zeros.See also csvwrite, dlmread, dlmwrite, load, fileformats, textscan.Reference page in Help browserdoc csvreadMatlab如何读取Excel 表格数据Subject:Are there any examples that show how to use the ActiveX automation interface to connect MATLAB to Excel?Problem DescriptionI 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 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 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 contains 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 workbook, change active worksheet,% get/put array, save, and close% First open an Excel ServerExcel = actxserver('Excel.Application');set(Excel, 'Visible', 1);% Insert a new workbookWorkbooks = Excel.Workbooks;Workbook = invoke(Workbooks, 'Add');% Make the second sheet activeSheets = Excel.ActiveWorkBook.Sheets;sheet2 = get(Sheets, 'Item', 2);invoke(sheet2, 'Activate');% Get a handle to the active sheetActivesheet = Excel.Activesheet;% Put a MATLAB array into ExcelA = [1 2; 3 4];ActivesheetRange = get(Activesheet,'Range','A1:B2');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 = reshape([B{:}], size(B));% Now save the workbookinvoke(Workbook, 'SaveAs', 'myfile.xls');% To avoid saving the workbook and being prompted to do so,% uncomment the following code.% Workbook.Saved = 1;% invoke(Workbook, 'Close');% Quit Excelinvoke(Excel, 'Quit');% End processdelete(Excel);There are several options for connecting MATLAB with Excel. For an example that shows how to connect MATLAB with Excel using Excel Link, please refer to the following URL:/support/solutions/data/27338.shtmlFor an example that shows how to connect MATLAB with Excel using DDE, please refer to the following URL:/support/solutions/data/31072.shtmlFor information on how to use the XLSREAD function to read .xls files, please refer to the following URL:/access/helpdesk/help/techdoc/ref/xlsread.shtml在Matlab GUI中读取数据或其它文件假设在GUI(in Matlab)中设计好一按钮,点击以后弹出对话框,并希望获取来自电脑上任一文件夹下的数据或其它文件。

matlab读取excel方法

matlab读取excel方法

matlab读取excel方法
在MATLAB中读取Excel文件,你可以使用readtable或readmatrix函数。

以下是使用这些函数的一些基本示例:1.
使用readtable读取Excel文件:
2.
matlab复制代码
% 读取Excel文件
T = readtable('你的文件
名.xlsx');
% 显示数据
disp(T);
readtable`函数将读取Excel文件并将其存储为一个表格(table)。

你可以使用各种MATLAB函数来查询、处理和分析这个表格。

3.
使用readmatrix读取Excel文件:
4.
matlab复制代码
% 读取Excel文件
M = readmatrix('你的文件
名.xlsx');
% 显示数据
disp(M);
readmatrix函数将读取Excel文件并将其存储为一个矩阵。

与readtable不同,readmatrix假设Excel文件中的所有数据都是数字,并将其转换为双精度浮点数。

如果你知道Excel文件中的数据类型,你可以使用readmatrix。

但是,如果你不确定数据类型,或者你的数据包含文本,那么你可能更喜欢使用readtable`。

注意:这些示例假设你的Excel文件是.xlsx格式的。

如果你的文件是.xls格式的,你可能需要使用不同的函数或方法来读取。

此外,为了读取Excel文件,你需要在MATLAB中安装Excel File Readers and Writers Toolbox。

xlsread函数用法

xlsread函数用法

xlsread函数用法
一. 简介
xlsread函数是MATLAB中用于读取Excel文件的函数,它可以从Excel文件中读取数据并将数据存储为MATLAB数组。

xlsread函数可以读取Excel文件中的数字、字符、公式等数据类型,并且可以读取任意大小的Excel文件。

二. 语法
xlsread函数的基本语法如下:
A = xlsread(filename,sheet,range)
参数说明:
- filename:Excel文件的名称或者包含文件路径的完整文件名。

- sheet:Excel中的工作表名称或索引号。

默认值为1,表示第一个工作表。

- range:指定待读取数据的范围。

可以指定单个单元格、单行、单列或多行多列的范围。

返回值:
- A:读取到的Excel数据,存储为MATLAB数组。

三. 示例
1. 读取整个Excel文件
为了演示xlsread函数的用法,先创建一个包含数据的Excel文件,然后使用xlsread函数读取这个文件。

```Matlab
A = xlsread('data.xlsx');
```
这段代码将读取名为\。

matlab关于excel的相关操作

matlab关于excel的相关操作

>>w = interp2(service,years,wage,15,1975)
插值结果为:
w=
190.6288
命令 3 interp3
功能 三维数据插值(查表)
格式 VI = interp3(X,Y,Z,V,XI,YI,ZI) %找出由参量 X,Y,Z 决定的三元函数 V=V(X,Y,Z)在点(XI,YI,ZI)的值。参量 XI,YI,ZI 是同型阵列或向量。若向量参 量 XI,YI,ZI 是不同长度,不同方向(行或列)的向量,这时输出参量 VI 与 Y1,Y2,Y3 为同型矩阵。其中 Y1,Y2,Y3 为用命令 meshgrid(XI,YI,ZI)生成的同型阵列。若插 值点(XI,YI,ZI)中有位于点(X,Y,Z)之外的点,则相应地返回特殊变量值 NaN。
yi = interp1(x,Y,xi,method,extrapval) %确定超出 x 范围的 xi 中的分量的外插 值 extrapval,其值通常取 NaN 或 0。 例 2-31 >>x = 0:10; y = x.*sin(x); >>xx = 0:.25:10; yy = interp1(x,y,xx); >>plot(x,y,'kd',xx,yy) 插值图形
=====================
在 Matlab 中排序某个向量(一维)时,可以使用 sort(A),其中 A 为待排序的向量,如果仅是用来排序 A,那么直接使用 sort (A)即可,如果排序后还需要保留原来的索引可以用返回值,即[B,ind]=sort(A),计算后,B 是 A 排序后的向量,A 保持不变,ind 是 B 中每一项对应于 A 中项的索引。排序是安升序进行的。

matlab读取excel

matlab读取excel

matlab读取exceloffice的表格文件也就是xls文件本质上就是一个二维矩阵,二维矩阵是用来保存数据的最佳方式,所以在日常工作中,我们从其它地方获取的数据通常都被保存为xls格式,但处理数据时,我们却需要把xls文件的数据导入到matlab里进行处理。

如果你只处理一个文件并且只做一次的话,你可以手动来拷贝粘贴,这花费不了你太多时间。

如果有很多xls文件,或者你的xls文件的内容可能随时被修改,那么下面的方法可以派上用场。

matlab自身提供了大量的函数,包括读取office文件。

其中xlsread和xlswrite就是专门用来读取xls文件里的数据的。

这两个函数的使用方法可以直接查看matlab自带的帮助。

xlsread对于纯数据的xls文件支持很完美,也就是说当xls文件里的每个格子都是“数”时,xlsread会直接返回一个实数矩阵。

但是通常我们拿到xls文件并不是这样,它的表头多半是描述性文字,它的数据也有可能是文字,有些位置的数据还有可能是缺失的。

xlsread 对这样的文件读取无能为力,或者说需要大量的时间去协调数据的位置信息。

要是有一个函数,能够按照原有的顺序直接读取所有的单位格数据就好了。

当然,这时候返回的矩阵就不能是一个数值矩阵了,它将会是一个cell矩阵,里面的每个元素类型可能不一样。

matlab本身并不提供这个功能,但是另外有一个函数officedoc完美的实现这个功能。

这个函数包可以去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 = actxserver('Excel.Application');set(Excel, 'Visible', 1);Workbooks = Excel.Workbooks;Workbook = invoke(Workbooks, 'Open', [cd,'\feature\ABC.xls']);%% 读取ABC.xls:sheet1 a1(即R1C1)~an40(即R240c40) 范围内的 40by40 矩阵read_excel=ddeinit('excel','ABC.xls:sheet1');feature1 = ddereq(read_excel, 'R1c1:R40c40');feature1%% 关闭ABC.xlsinvoke(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 data specified in the Excelfile, FILE. The numeric cells in FILE are returned in NUMERIC, the textcells in FILE are returned in TXT, while the raw, unprocessed cellcontent is returned in RAW.[NUMERIC,TXT,RAW]=XLSREAD(FILE,SHEET,RANGE) reads the data specifiedin RANGE from the worksheet SHEET, in the Excel file specified in FILE.It is possible to select the range of data interactively (see Examplesbelow). Please note that the full functionality of XLSREAD depends onthe ability to start Excel as a COM server from MATLAB.[NUMERIC,TXT,RAW]=XLSREAD(FILE,SHEET,RANGE,'basic') reads an XLS file asabove, using basic input mode. This is the mode used on UNIX platformsas 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 limitsimport ability. Without Excel as a COM server, RANGE will be ignoredand, consequently, 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,'',CUSTOMFUN) When the Excel COM server is used, allows passing in a handle to acustom function. This function will be called just before retrievingthe actual data from Excel. It must take an Excel Range object (e.g. oftype 'Interface.Microsoft_Excel_5.0_Object_Library.Range') as input,and return one as output. Optionally, this custom function may returna second output argument, which will be returned from XLSREAD as thefourth output argument, CUSTOMOUTPUT. For details of what is possibleusing 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'.SHEET: string defining worksheet name in workbook FILE.double scalar defining worksheet index in workbook FILE. SeeNOTE 1.RANGE: string defining the data range in a worksheet. See NOTE 2.MODE: string enforcing basic import mode. Valid value = 'basic'. Thisis the mode always used when COM is not 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 = xlsread(FILE);[NUMERIC,TXT]=xlsread(FILE);[NUMERIC,TXT,RAW]=xlsread(FILE);2. Get data from the default region:NUMERIC = xlsread('c:\matlab\work\myspreadsheet')3. Get data from the used area in a sheet other than the first sheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2')4. Get data from a named sheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData')5. Get data from a specified region in a sheet other than the firstsheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2','a2:j5')6. Get data from a specified region in a named sheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData','a2:j5')7. Get data from a region in a sheet specified by index:NUMERIC = xlsread('c:\matlab\work\myspreadsheet',2,'a2:j5')8. Interactive region selection:NUMERIC = xlsread('c:\matlab\work\myspreadsheet',-1);You have to select the active region and the active sheet in theEXCEL window that will come into focus. Click OK in the DataSelection Dialog when you have finished selecting the active region.9. Using the custom function:[NUMERIC,TXT,RAW,CUSTOMOUTPUT] = xlsread('equity.xls', ..., @MyCustomFun) Where the CustomFun is defined as:function [DataRange, customOutput] = MyCustomFun(DataRange)DataRange.NumberFormat = 'Date';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 SHEET is -1, Excel comes to the foreground to enable interactiveselection (optional). In interactive mode, a dialogue will promptyou to click the OK button in 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 in a worksheet. RANGE is not case sensitive and uses Excel A1notation (see Excel Help). (Only supported when Excel COM serveris available.)NOTE 3: Excel formats other 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 browserdoc xlsreadCSVREAD 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 containnumeric 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.M = CSVREAD('FILENAME',R,C,RNG) reads only the range specifiedby RNG = [R1 C1 R2 C2] where (R1,C1) is the upper-left corner ofthe data to be read and (R2,C2) is the lower-right corner. RNGcan also be specified using spreadsheet notation as in RNG = 'A1..B7'.CSVREAD fills empty delimited fields with zero. Data files wherethe lines end with a comma will produce a result with an extra lastcolumn filled with zeros.See also csvwrite, dlmread, dlmwrite, load, fileformats, textscan.Reference page in Help browserdoc csvreadMatlab如何读取Excel 表格数据Subject:Are there any examples that show how to use the ActiveX automation interface to connect MATLAB to Excel?Problem DescriptionI 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 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 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 contains 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 workbook, change active worksheet,% get/put array, save, and close% First open an Excel ServerExcel = actxserver('Excel.Application');set(Excel, 'Visible', 1);% Insert a new workbookWorkbooks = Excel.Workbooks;Workbook = invoke(Workbooks, 'Add');% Make the second sheet activeSheets = Excel.ActiveWorkBook.Sheets;sheet2 = get(Sheets, 'Item', 2);invoke(sheet2, 'Activate');% Get a handle to the active sheetActivesheet = Excel.Activesheet;% Put a MATLAB array into ExcelA = [1 2; 3 4];ActivesheetRange = get(Activesheet,'Range','A1:B2');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 = reshape([B{:}], size(B));% Now save the workbookinvoke(Workbook, 'SaveAs', 'myfile.xls');% To avoid saving the workbook and being prompted to do so,% uncomment the following code.% Workbook.Saved = 1;% invoke(Workbook, 'Close');% Quit Excelinvoke(Excel, 'Quit');% End processdelete(Excel);There are several options for connecting MATLAB with Excel. For an example that shows how to connect MATLAB with Excel using Excel Link, please refer to the following URL:/support/solutions/data/27338.shtmlFor an example that shows how to connect MATLAB with Excel using DDE, please refer to the following URL:/support/solutions/data/31072.shtmlFor information on how to use the XLSREAD function to read .xls files, please refer to the following URL:/access/helpdesk/help/techdoc/ref/xlsread.shtml在Matlab GUI中读取数据或其它文件假设在GUI(in Matlab)中设计好一按钮,点击以后弹出对话框,并希望获取来自电脑上任一文件夹下的数据或其它文件。

matlab基础xlsread

matlab基础xlsread

在一篇文章里看到了MATLAB对Excel文件的操作。

当然,最简单的是选状态栏中的Import 了,在这里作者提供了两种读取Excel文件的方法。

1 利用matlab从excel中读取数据使用1.1 uiimport可视化导入命令只需要在command window中输入uiimport,出现这个窗口后你就懂了1.2 用xlsread读Excel,不仅适合后缀为xls的文件,07版xlsx也适用假设excel表的文件名为book1.xls,存放在E:\读取命令:data = xlsread('E:\book1');这样就将excel中的数据读入到data中了,但这个命令只能用来读数值,无法读取文字。

如果读取数值的同时想读取excel中的文字可以使用下面的命令:[data,text] = xlsread('E:\book1');命令执行后数值在data数组中,文字在text中,text为cell类型xlsread命令参数如下data = xlsread(filename, sheet, range)sheet代表从哪个表中读入数据,range代表读入数据区间例如:'A2:D4'2. 用matlab将数据写入excel中xlswrite(filename, M, sheet, range)例:xlswrite('E:\text.xls',M,sheet2,'A3:E5') M为要写入的数据,可以是矩阵也可以是cell 类型注:MATLAB中读写Excel的函数有xlsfinfo检查文件是否包含excel表格;xlread读写excel文件;xlswrite写excel文件。

xlsread的调用格式为:1. num=xlsread('filename')从excel文件filename的第一个工作页中读取所有的数据到double 型数据num中。

xlsread函数

xlsread函数

xlsread函数xlsread函数是MATLAB中用于读取Excel文件的函数。

通过这个函数,用户可以方便地读取Excel文件中的数据,进行进一步的处理和分析。

下面我们来详细介绍一下xlsread函数的使用方法和功能。

一、函数的语法xlsread(filename, sheet, range)其中,filename表示要读取的Excel文件的文件名,可以包含路径信息。

sheet表示要读取的工作表的名称或索引。

range表示要读取的单元格范围。

二、参数的详细说明1. filename:要读取的Excel文件的文件名,必须是一个字符串。

如果filename参数未指定路径,则xlsread函数从当前文件夹中搜索文件。

如果filename参数包含了路径,则xlsread函数将直接查找指定的文件。

2. sheet:要读取的工作表的名称或索引。

如果不指定该参数,则默认读取第一个工作表。

如果指定了一个名称,则xlsread函数将对工作表名称进行不区分大小写的匹配。

如果指定了一个索引,则xlsread函数将读取该索引对应的工作表。

3. range:要读取的Excel单元格范围。

范围可以用Excel的A1表示法或R1C1表示法指定。

如果省略该参数,则默认为'A1'。

三、函数的返回值xlsread函数将返回一个由读取数据组成的矩阵。

读取的数据可以是数值、文本或日期类型。

如果Excel文件中包含公式,则xlsread函数将返回该公式计算出的值。

如果读取的单元格为空,则返回值将为一个NaN(not a number)。

除了读取数据之外,xlsread函数还可以返回工作表中的其他信息,如工作表名称、单元格的数据类型等。

用户只需要在函数调用中指定需要返回的参数即可。

四、使用示例下面我们来看一个简单的示例,演示如何使用xlsread函数读取Excel文件的数据。

假设我们有一个名为example.xlsx的Excel文件,其中包含一个名为Sheet1的工作表,表中有以下数据:```1 2 34 5 67 8 9```现在我们要读取这个Excel文件中的数据。

matlab中的数据读取

matlab中的数据读取


% 判断首字符是否是数值

fprintf(fidout,'%s\n\n',tline);

% 如果是数字行,把此行数据写入文件MKMATLAB.txt

continue
% 如果是非数字继续下一次循环
❖ end
❖ end
❖ fclose(fidout);
❖ MK=importdata('MKMATLAB.txt');
❖ 谢谢观看
数据读取
一 、将excel数据导入matlab
❖ 1.直接导入
❖在文件菜单中选择 file/import data, ❖ 按照提示进行操作至结束。(book1.xls)
❖ >> Sheet1 ❖ Sheet1 = ❖ 123 ❖ 456
2.xlsread函数导入
❖ [filename, pathname]=uigetfile('*.xls'); ❖ %寻找源文件
'attrib2', value2, ...) ❖ dlmwrite(filename, M, '-append') ❖ dlmwrite(filename, M, '-append', attribute-
value list)
❖>> a=magic(5); ❖>> dlmwrite('test3.txt',a)
❖ >> M = magic(3); ❖ >> dlmwrite('test5.txt', [M*5 M/5], ' ') ❖ >> !type test5.txt ❖ 40 5 30 1.6 0.2 1.2 ❖ 15 25 35 0.6 1 1.4 ❖ 20 45 10 0.8 1.8 0.4
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

matlab读取exceloffice的表格文件也就是xls文件本质上就是一个二维矩阵,二维矩阵是用来保存数据的最佳方式,所以在日常工作中,我们从其它地方获取的数据通常都被保存为xls格式,但处理数据时,我们却需要把xls文件的数据导入到matlab里进行处理。

如果你只处理一个文件并且只做一次的话,你可以手动来拷贝粘贴,这花费不了你太多时间。

如果有很多xls文件,或者你的xls文件的内容可能随时被修改,那么下面的方法可以派上用场。

matlab自身提供了大量的函数,包括读取office文件。

其中xlsread和xlswrite就是专门用来读取xls文件里的数据的。

这两个函数的使用方法可以直接查看matlab自带的帮助。

xlsread对于纯数据的xls文件支持很完美,也就是说当xls文件里的每个格子都是“数”时,xlsread会直接返回一个实数矩阵。

但是通常我们拿到xls文件并不是这样,它的表头多半是描述性文字,它的数据也有可能是文字,有些位置的数据还有可能是缺失的。

xlsread 对这样的文件读取无能为力,或者说需要大量的时间去协调数据的位置信息。

要是有一个函数,能够按照原有的顺序直接读取所有的单位格数据就好了。

当然,这时候返回的矩阵就不能是一个数值矩阵了,它将会是一个cell矩阵,里面的每个元素类型可能不一样。

matlab本身并不提供这个功能,但是另外有一个函数officedoc完美的实现这个功能。

这个函数包可以去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 = actxserver('Excel.Application');set(Excel, 'Visible', 1);Workbooks = Excel.Workbooks;Workbook = invoke(Workbooks, 'Open', [cd,'\feature\ABC.xls']);%% 读取ABC.xls:sheet1 a1(即R1C1)~an40(即R240c40) 范围内的 40by40 矩阵read_excel=ddeinit('excel','ABC.xls:sheet1');feature1 = ddereq(read_excel, 'R1c1:R40c40');feature1%% 关闭ABC.xlsinvoke(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 data specified in the Excelfile, FILE. The numeric cells in FILE are returned in NUMERIC, the textcells in FILE are returned in TXT, while the raw, unprocessed cellcontent is returned in RAW.[NUMERIC,TXT,RAW]=XLSREAD(FILE,SHEET,RANGE) reads the data specifiedin RANGE from the worksheet SHEET, in the Excel file specified in FILE.It is possible to select the range of data interactively (see Examplesbelow). Please note that the full functionality of XLSREAD depends onthe ability to start Excel as a COM server from MATLAB.[NUMERIC,TXT,RAW]=XLSREAD(FILE,SHEET,RANGE,'basic') reads an XLS file asabove, using basic input mode. This is the mode used on UNIX platformsas 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 limitsimport ability. Without Excel as a COM server, RANGE will be ignoredand, consequently, 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,'',CUSTOMFUN) When the Excel COM server is used, allows passing in a handle to acustom function. This function will be called just before retrievingthe actual data from Excel. It must take an Excel Range object (e.g. oftype 'Interface.Microsoft_Excel_5.0_Object_Library.Range') as input,and return one as output. Optionally, this custom function may returna second output argument, which will be returned from XLSREAD as thefourth output argument, CUSTOMOUTPUT. For details of what is possibleusing 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'.SHEET: string defining worksheet name in workbook FILE.double scalar defining worksheet index in workbook FILE. SeeNOTE 1.RANGE: string defining the data range in a worksheet. See NOTE 2.MODE: string enforcing basic import mode. Valid value = 'basic'. Thisis the mode always used when COM is not 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 = xlsread(FILE);[NUMERIC,TXT]=xlsread(FILE);[NUMERIC,TXT,RAW]=xlsread(FILE);2. Get data from the default region:NUMERIC = xlsread('c:\matlab\work\myspreadsheet')3. Get data from the used area in a sheet other than the first sheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2')4. Get data from a named sheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData')5. Get data from a specified region in a sheet other than the firstsheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','sheet2','a2:j5')6. Get data from a specified region in a named sheet:NUMERIC = xlsread('c:\matlab\work\myspreadsheet','NBData','a2:j5')7. Get data from a region in a sheet specified by index:NUMERIC = xlsread('c:\matlab\work\myspreadsheet',2,'a2:j5')8. Interactive region selection:NUMERIC = xlsread('c:\matlab\work\myspreadsheet',-1);You have to select the active region and the active sheet in theEXCEL window that will come into focus. Click OK in the DataSelection Dialog when you have finished selecting the active region.9. Using the custom function:[NUMERIC,TXT,RAW,CUSTOMOUTPUT] = xlsread('equity.xls', ..., @MyCustomFun) Where the CustomFun is defined as:function [DataRange, customOutput] = MyCustomFun(DataRange)DataRange.NumberFormat = 'Date';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 SHEET is -1, Excel comes to the foreground to enable interactiveselection (optional). In interactive mode, a dialogue will promptyou to click the OK button in 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 in a worksheet. RANGE is not case sensitive and uses Excel A1notation (see Excel Help). (Only supported when Excel COM serveris available.)NOTE 3: Excel formats other 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 browserdoc xlsreadCSVREAD 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 containnumeric 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.M = CSVREAD('FILENAME',R,C,RNG) reads only the range specifiedby RNG = [R1 C1 R2 C2] where (R1,C1) is the upper-left corner ofthe data to be read and (R2,C2) is the lower-right corner. RNGcan also be specified using spreadsheet notation as in RNG = 'A1..B7'.CSVREAD fills empty delimited fields with zero. Data files wherethe lines end with a comma will produce a result with an extra lastcolumn filled with zeros.See also csvwrite, dlmread, dlmwrite, load, fileformats, textscan.Reference page in Help browserdoc csvreadMatlab如何读取Excel 表格数据Subject:Are there any examples that show how to use the ActiveX automation interface to connect MATLAB to Excel?Problem DescriptionI 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 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 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 contains 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 workbook, change active worksheet,% get/put array, save, and close% First open an Excel ServerExcel = actxserver('Excel.Application');set(Excel, 'Visible', 1);% Insert a new workbookWorkbooks = Excel.Workbooks;Workbook = invoke(Workbooks, 'Add');% Make the second sheet activeSheets = Excel.ActiveWorkBook.Sheets;sheet2 = get(Sheets, 'Item', 2);invoke(sheet2, 'Activate');% Get a handle to the active sheetActivesheet = Excel.Activesheet;% Put a MATLAB array into ExcelA = [1 2; 3 4];ActivesheetRange = get(Activesheet,'Range','A1:B2');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 = reshape([B{:}], size(B));% Now save the workbookinvoke(Workbook, 'SaveAs', 'myfile.xls');% To avoid saving the workbook and being prompted to do so,% uncomment the following code.% Workbook.Saved = 1;% invoke(Workbook, 'Close');% Quit Excelinvoke(Excel, 'Quit');% End processdelete(Excel);There are several options for connecting MATLAB with Excel. For an example that shows how to connect MATLAB with Excel using Excel Link, please refer to the following URL:/support/solutions/data/27338.shtmlFor an example that shows how to connect MATLAB with Excel using DDE, please refer to the following URL:/support/solutions/data/31072.shtmlFor information on how to use the XLSREAD function to read .xls files, please refer to the following URL:/access/helpdesk/help/techdoc/ref/xlsread.shtml在Matlab GUI中读取数据或其它文件假设在GUI(in Matlab)中设计好一按钮,点击以后弹出对话框,并希望获取来自电脑上任一文件夹下的数据或其它文件。

相关文档
最新文档