如何用matlab处理excel文件中的数据

如题所述

matlab读取excel文件可用xlsread函数,向excel中写数据可用xlswrite函数。
注意:matlab不识别中文,读写的文件中最好不含有中文。

举例说明如下:
1、matlab读取excel文件数据:
bb = xlsread('c:\feature.xls', 'A0:A40');
其中:
c:\feature.xls为文件存放的地址;
A0:A40为将要读取的单元格的范围;
bb为读取的矩阵在MATLAB中的变量名。

2、matlab向excel文件写数据:
a1={'number'};
b1={'scoreA'};
c1={'scoerB'};
A=rand(100,3).*100;
xlswrite('a.xls',a1,'Sheet1','A1'); % a.xls是文件名,Sheet1是xls中第一个页面,A1为写入位置,下同
xlswrite('a.xls',b1,'Sheet1','B1');
xlswrite('a.xls',c1,'Sheet1','C1');
xlswrite('a.xls',A,'Sheet1','A2:C101');
温馨提示:内容为网友见解,仅供参考
第1个回答  2015-07-02
在Matlab命令行窗口使用help使用帮助命令
help xlsread
xlsread Read Microsoft Excel spreadsheet file.
[NUM,TXT,RAW]=xlsread(FILE) reads data from the first worksheet in
the Microsoft Excel spreadsheet file named FILE and returns the numeric
data in array NUM. Optionally, returns the text fields in cell array
TXT, and the unprocessed data (numbers and text) in cell array RAW.

[NUM,TXT,RAW]=xlsread(FILE,SHEET) reads the specified worksheet.

[NUM,TXT,RAW]=xlsread(FILE,SHEET,'','basic') reads from the
spreadsheet in BASIC mode, the default on systems without Excel
software.

The following syntaxes are supported only on Windows systems with Excel
software:

[NUM,TXT,RAW]=xlsread(FILE,-1) opens an Excel window to select data
interactively.

[NUM,TXT,RAW]=xlsread(FILE,RANGE) reads data from the specified
RANGE of the first worksheet in the file. Specify RANGE using the
syntax 'C1:C2', where C1 and C2 are opposing corners of the region.

[NUM,TXT,RAW]=xlsread(FILE,SHEET,RANGE) reads from the specified SHEET
and RANGE.

[NUM,TXT,RAW,CUSTOM]=xlsread(FILE,SHEET,RANGE,'',FUNCTIONHANDLE)
reads from the spreadsheet, executes the function associated with
FUNCTIONHANDLE on the data, and returns the final results. Optionally,
returns additional CUSTOM output, which is the second output from the
function. xlsread does not change the data stored in the spreadsheet.

Input Arguments:

FILE String that specifies the file to read. If you do not include
an extension, xlsread searches for a file with the specified
name and the extension '.xls'.
-1 Flag to open an interactive Excel window for selecting data.
Select the worksheet, drag and drop the mouse over the range
you want, and click OK. Supported only on Windows systems with
Excel software.
SHEET Worksheet to read. One of the following:
* String that contains the worksheet name.
* Positive, integer-valued scalar indicating the worksheet
index. Supported only on Windows systems with Excel software.
RANGE String that specifies a rectangular portion of the worksheet to
read. Not case sensitive. Use Excel A1 reference style.
If you do not specify a SHEET, RANGE must include both corners
and a colon character (:), even for a single cell (such as
'D2:D2').
'basic' Flag to request reading in basic mode, which is the default for
systems without Excel for Windows. In basic mode, xlsread:
* Only reads XLS files compatible with Excel 97-2003 software.
* Imports the entire active range of the worksheet.
* Requires a string to specify the SHEET, and the name is case
sensitive.
* Does not support function handle inputs.
FUNCTIONHANDLE
Handle to your custom function. When xlsread calls your
function, it passes a range interface from Excel to provide
access to the data. Your function must include this interface
(of type 'Interface.Microsoft_Excel_5.0_Object_Library.Range',
for example) both as an input and output argument.
从上述帮助文件可以看到相关参数及用法
例如:
假若有e:/myfile.xls文件
生日 姓名 学号

2015年7月2日 张三 8989009898
则:

读取:

filename = 'e:/myfile';%文件名
%读数值用,将数据保存到data矩阵中
data = xlsread(filename );
%若想读数值和文字信息
[data,text] = xlsread('E:\book1');

data =

8.9890e+009

text =

'生日' '姓名' '学号'
'2015-7-2' '张三' ''

写入:

xlswrite(FILE,ARRAY,SHEET,RANGE) writes to the specified SHEET and
RANGE.

在myfile.xls第一页的c4坐标写入数字1,则代码如下:

xlswrite(filename,1,1,'c4');
结果如下:
生日 姓名 学号

2015年7月2日 张三 8989009898

1
myfile.xls第一页的c4坐标写入矩阵[1,2],则代码如下:
xlswrite(filename,[1,3],1,'c4');%c4表示写入数据位置的左上角坐标,也可自己设定写入区域
结果如下:
生日 姓名 学号

2015年7月2日 张三 8989009898

1 3
第2个回答  2015-07-02
假设你的excel文件名为Example.xlsx
那么可以用下面的语句把整个表单的数据读成一个矩阵A

filename = 'Example.xlsx';
A = xlsread(filename);本回答被提问者和网友采纳
相似回答