matlab与excel xlsread、xlswrite实用方法

 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'
原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/12007807.html