MATLAB如何读取excel文件中的数据

合集下载

Matlab提取Excel文件中部分内容

Matlab提取Excel文件中部分内容

Matlab提取Excel文件中部分内容Excel表格整表操作十分方便,xlsread函数支持对表单的操作,也支持对某个矩形区域的操作。

比如要求把一表格的'A4:A6','C2:C4','E3:E5'的内容连续保存在新表格中。

连续保存没什么,重点是将不连续的区域读出来,虽然多次读取可以实现,但是如果读取海量表格中的几百列,几千这种方法就没有效率可言了,所以需要将表格的标号放到循环中去处理,让循环来代替手动读取,这样即使成千上万遍的读取也没有问题。

代码如下:复制内容到剪贴板代码:str={'A4:A6','C2:C4','E3:E5'};data=[];for ii=1:3A = xlsread('ff.xls', 1, str{ii});data=[data,A];enddata中保存的就是从不连续的区域读取来的内容。

上例中虽然读取方便,但是位置字符串设置仍不够方便,如果设置几百个或者几千个字符串位置,也够累的,所以可以将字符串中的数字也用循环变量来代替。

复制内容到剪贴板代码:str=['A','C'];data=[];for jj=1:2;for ii=1:3A = xlsread('ff.xls', 1, [str(jj),num2str(ii)])data=[data,A];endend上述就是读取A1:A3和C1:C3单元的内容。

filename1=ls('*xls');%取得所用excel文件filename=cellstr(filename1);num=length(filename);%得到excel文件的个数for ii=1:numM=xlsread(filename{ii});%执行对矩阵M的处理代码enddata=load('路径\文件.xls');data=xlsread('路径\文件.xls');可以通过Excel link 将Excel和MATLAB链接起来,就可以将Excel里面的数据以矩阵的形式导入MATLAB的工作区间workspace中,Excel link 的安装(1)启动Excel(2)单击工具加载宏命令,在弹出的加载宏对话窗口中单击浏览按钮。

matlab中各种数据的读取

matlab中各种数据的读取

matlab中各种数据的读取
在MATLAB中,可以使用不同的函数来读取各种类型的数据。

以下是一些常用的数据读取函数:
1. 读取文本文件:
- `textread`:读取格式化的文本文件。

- `fscanf`:按指定格式读取文本文件。

- `importdata`:读取文本文件,并将数据存储在结构体中。

2. 读取电子表格文件:
- `xlsread`:读取 Microsoft Excel 文件。

- `readtable`:读取 Excel、CSV 或者其他格式的表格文件,并将数据存储在表格对象中。

3. 读取图象文件:
- `imread`:读取图象文件,并将其存储为图象矩阵。

- `dicomread`:读取 DICOM 文件,并将其存储为图象矩阵。

4. 读取音频文件:
- `audioread`:读取音频文件,并将其存储为音频信号向量。

5. 读取视频文件:
- `VideoReader`:创建一个视频阅读器对象,用于逐帧读取视频文件。

6. 读取二进制文件:
- `fread`:按指定格式读取二进制文件,并将数据存储在矩阵中。

以上只是一些常用的数据读取函数,MATLAB还提供了许多其他函数,用于读取和处理各种类型的数据。

具体的使用方法可以参考MATLAB的文档或者在线匡助。

excel readtable函数的用法matlab

excel readtable函数的用法matlab

excel readtable函数的用法matlab在MATLAB中,没有直接与Excel的"READTABLE"函数相对应的函数。

但是,您可以使用"readmatrix"或"readarray"函数来读取Excel文件中的数据。

这些函数可以读取Excel文件中的单元格数据,并将其存储为矩阵或数组。

以下是使用"readmatrix"函数读取Excel文件的示例代码:```matlabfilename = 'example.xlsx';sheet = 1;range = 'A1:C10';data = readmatrix(filename, sheet, range);```在上面的代码中,您需要将"filename"替换为您要读取的Excel文件的文件名,"sheet"替换为要读取的工作表的索引或名称,"range"替换为要读取的单元格范围的地址。

读取的数据将被存储在名为"data"的矩阵中。

如果您需要读取的Excel文件中的数据是表格格式,则可以使用"readarray"函数。

以下是使用"readarray"函数读取Excel文件的示例代码:```matlabfilename = 'example.xlsx';sheet = 1;range = 'A1:C10';data = readarray(filename, sheet, range);```与"readmatrix"函数类似,您需要将"filename"、"sheet"和"range"替换为您要读取的Excel文件的文件名、要读取的工作表的索引或名称以及要读取的单元格范围的地址。

如何将Excel中的数据导入MATLAB并在MATLAB中画出图象

如何将Excel中的数据导入MATLAB并在MATLAB中画出图象

1.如何将Excel中的数据在MATLAB界面下显示出来:
首先Excel必须是office的(WPS的不可以,MATLAB不能识别),在MATLAB中使用命令xlsread读取Excel中的数据到MATLAB里,如下所示:[number,txt,raw]=xlsread('noise.xlsx'),此
时读取的文件须在MATLAB默认路径下,命令具体用法及参数含
义可直接用help命令在MATLAB中查找。

读取的数据界面如下:
2.如何将Excel中的数据导入MATLAB并在MATLAB中画出图像?
首先Excel必须是office的(WPS的不可以,MATLAB不能识
别),file-Import Data-在工作路径下找到需要导入的文件(图3)-点击打开(图4)-点击next-点击finish-导入成功-在workspace中看到图5(导入的数据)。

至此数据导入成功。

下面是如何利用该数据绘图:
法一:双击workspace中的data打开如图6,选中要画图的数据如图7(数据范围可以根据需要随便选择),点击上方的
plot(data(1:874,1:2),画出图像如图8,并且在工作空间中自动显示画图命令。

法二:直接在工作空间中写画图命令。

图3
图4
图5
图6
图7
图8。

如何将Excel中的数据导入MATLAB并在MATLAB中画出图像

如何将Excel中的数据导入MATLAB并在MATLAB中画出图像

1. 如何将Excel中的数据在MATLA界面下显示出来:首先Excel必须是office 的(WPS勺不可以,MATLA不能识别),在MATLA中使用命令xlsread读取Excel中的数据到MATLA里,如下所示:[number,txt,raw]=xlsread('noise.xlsx'),此时读取的文件须在MATLA默认路径下,命令具体用法及参数含义可直接用help 命令在MATLA中查找。

读取的数据界面如下:[0.2i60][0.24SO][0.2500][0.2520][0.2540][0.2560][0.2580][0.25Q0][0.2620][0. 2640][0. 2660][0.2680][0.2700][0.2720][0.2740][0. 2760][23.6000][ 28][30.4000]125-2000][ 18][19.2000][2L 2000][17. 20Q0][16.SOOO][17.6000][13.6000][11.2000][13.6000][13.2000][ 10][12.4000]2. 如何将Excel中的数据导入MATLA并在MATLA中画出图像?首先Excel必须是office 的(WPS勺不可以,MATLA不能识别),file-Import Data- 在工作路径下找到需要导入的文件(图3)-点击打开(图4)-点击next-点击finish- 导入成功-在workspace中看到图5 (导入的数据)。

至此数据导入成功。

下面是如何利用该数据绘图:法一:双击workspace中的data打开如图6,选中要画图的数据如图7 (数据范围可以根据需要随便选择),点击上方的plot(data(1:874,1:2),画出图像如图8,并且在工作空间中自动显示画图命令。

法二:直接在工作空间中写画图命令。

>> [number, txt raw!=zlsr*ad C noise.xlsz J)riunber 二0.246023. 60000.24SC 2& 00000.2500 30. 40000. 2520 25.20000. 2E40 18. 00000.2560 2000Di 2580 21. 20000.2600 17.20000. 2620 16.30000. 2640 17. 60000. 13. 60000* 26SA 1],20000. 2700 13. SQ00Import Data査找范圉(X):库计算机网貉名称'悸改日期,,noise.rriat2014/3/1S 19:181^gnoisenKlsx2014/3/1& 19:01I HKTIAB ▼> ®文件名曲):iriOL XISX文件类型⑴;Recognized Filww取消 |JJWorkspace圄■鱼1鲂I^Lr Select data ...〒Name *田[甌Value<874x2 double* rrrCommand Window■+* □ ? X>> plol (datadia?^ I. = 2), J DisplayMafte J p J dataCl;874? 1;2)a ? J YDataSource 1/ data(i:B74? 1:2^ ) .f igurt (ecf)九—K —图81 f 4New to MATLAB? Watch thi$ Sdg see 口旳口轧 or read Getting 5t^rt 亡cLNew Parallel D”ktq 因❼Ctirrent F file Edit 0ew Ins-ert lo&ls Desktop Window Help□ U 矗B |輿耳U 物穏摇TO □囲I ■口IXCommaHd Window。

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。

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)中设计好一按钮,点击以后弹出对话框,并希望获取来自电脑上任一文件夹下的数据或其它文件。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.读取sheet1中的所有数据
引用的文件名要包括后缀.xls,同时xls文件要位于当前搜索路径中(默认安装目录下work 文件夹中)才可以引用excel数据
默认这些文本以及数字都放在sheet1中,我们将此excel选入当前工作目录(必要步骤),然后再MA TLAB主窗口中输入下面命令:[NUM,TXT,RAW]=xlsread('example'),其中example是你的excel名,这里我所有的数据都在example.xls中。

输入以上命令,回车
NUM返回的是excel中的数据,TXT输出的是文本内容,RAW输出的是未处理数据,这里因为文本占用的矩阵一共是8行6列,所以就输出了如上内容。

一般情况下,我们读取的都是excel中的数据,所以可以直接用[NUM]=xlsread('example'),只输出数据矩阵便可。

2读取指定sheet中的数据
假如我们想读取第二个sheet中的数据,这里请注意:matlab读取excel中的数据是按照sheet在excel中的排放顺序来的,sheet3现在排在第二位,我们输入命令[NUM]=xlsrea d('example',2),回车
3 读取指定单元格中的数据
假设我们读取A3-D7之间的数据,那么我们输入命令[NUM]=xlsread('example',2,'A3:D7'),回车。

相关文档
最新文档