zoukankan      html  css  js  c++  java
  • 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'
  • 相关阅读:
    How to fix “X: user not authorized to run the X server, aborting.”? -摘自网络
    Running a Remote Desktop on a Windows Azure Linux VM (远程桌面到Windows Azure Linux )-摘自网络(试了,没成功
    Linux虚拟机创建后如何进行登录(Windows Azure)
    window.parent
    .net的.aspx页面调试方法
    continue的作用
    .NET中,在方法参数的类型前加一个OUT是做什么用的
    mssqlserver SQL注释快捷键
    物流英语
    安装Spring报错An error occurred while collecting items to be installed
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/12007807.html
Copyright © 2011-2022 走看看