• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

matlab与excel xlsread、xlswrite实用方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 from:http://apps.hi.baidu.com/share/detail/20827715

 

excel数据读取函数:

[num, txt, raw, X] = xlsread(filename, sheet, range)

filename, sheet, range 文件名,sheet名、范围

num, txt, raw, X 输出

excel数据写入函数:

[status, message] =xlswrite(filename, M, sheet, range)

filename, M, sheet, range 文件名,数据、sheet名、范围

Examples

Example 1— Reading Numeric Data

The Microsoft Excel spreadsheet file testdata1.xls contains this data:

1    6
    2    7
    3    8
    4    9
    5   10

To read this data into MATLAB, use this command:

A = xlsread(\'testdata1.xls\')
A =
    1    6
    2    7
    3    8
    4    9
    5   10

Example 2 — Handling Text Data

The Microsoft Excel spreadsheet file testdata2.xls contains a mix of numeric and text data:

1    6
    2    7
    3    8
    4    9
    5   text

xlsread puts a NaN in place of the text data in the result:

A = xlsread(\'testdata2.xls\')
A =
    1    6
    2    7
    3    8
    4    9
    5   NaN

Example 3 — Selecting a Range of Data

To import only rows 4 and 5 from worksheet 1, specify the range as \'A4:B5\':

A = xlsread(\'testdata2.xls\', 1, \'A4:B5\')

A =
    4    9
    5   NaN

Example 4 — Handling Files with Row or Column Headers

A Microsoft Excel spreadsheet labeled Temperatures in file tempdata.xls contains two columns of numeric data with text headers for each column:

Time  Temp
12     98
13     99
14     97

If you want to import only the numeric data, use xlsread with a single return argument. Specify the filename and sheet name as inputs.

xlsread ignores any leading row or column of text in the numeric result.

ndata = xlsread(\'tempdata.xls\', \'Temperatures\')

ndata =
    12    98
    13    99
    14    97

To import both the numeric data and the text data, specify two return values for xlsread:

[ndata, headertext] = xlsread(\'tempdata.xls\', \'Temperatures\')

ndata =
    12    98
    13    99
    14    97

headertext =
    \'Time\'    \'Temp\'
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Examples

Example 1 — Writing Numeric Data to the Default Worksheet

Write a 7-element vector to Microsoft Excel file testdata.xls. By default, xlswrite writes the data to cells A1 through G1 in the first worksheet in the file:
xlswrite(\'testdata.xls\', [12.7 5.02 -98 63.9 0 -.2 56])

Example 2 — Writing Mixed Data to a Specific Worksheet

This example writes the following mixed text and numeric data to the file tempdata.xls:

d = {\'Time\', \'Temp\'; 12 98; 13 99; 14 97};

Call xlswrite, specifying the worksheet labeled Temperatures, and the region within the worksheet to write the data to. xlswrite writes the 4-by-2 matrix to the rectangular region that starts at cell E1 in its upper left corner:

s = xlswrite(\'tempdata.xls\', d, \'Temperatures\', \'E1\')
s =
    1

The output status s shows that the write operation succeeded. The data appears as shown here in the output file:

Time   Temp
  12     98
  13     99
  14     97

Example 3 — Appending a New Worksheet to the File

Now write the same data to a worksheet that doesn’t yet exist in tempdata.xls. In this case, xlswrite appends a new sheet to the workbook, calling it by the name you supplied in the sheets input argument, \'NewTemp\'. xlswrite displays a warning indicating that it has added a new worksheet to the file:

xlswrite(\'tempdata.xls\', d, \'NewTemp\', \'E1\')
Warning: Added specified worksheet.

If you don’t want to see these warnings, you can turn them off with this command:

warning off MATLAB:xlswrite:AddSheet

Now try the write command again, this time creating another new worksheet, NewTemp2. Although the message does not appear this time, you can still retrieve it and its identifier from the second output argument, msg:

[stat msg] = xlswrite(\'tempdata.xls\', d, \'NewTemp2\', \'E1\');

msg
msg =
       message: \'Added specified worksheet.\'
    identifier: \'MATLAB:xlswrite:AddSheet\'

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap