Working with Spreadsheets

Microsoft® Excel® Spreadsheets

This section covers

For more detailed information and examples, see the xlsfinfo, xlswrite, and xlsread reference pages.

Getting Information About the File

Use the xlsfinfo function to determine if a file contains a readable Microsoft® Excel® spreadsheet.

Inputs to xlsfinfo are:

Outputs from xlsfinfo are:

Example — Querying an XLS File.   This example returns information about spreadsheet file tempdata.xls:

[type, sheets] = xlsfinfo('tempdata.xls')

type =
Microsoft Excel Spreadsheet
sheets = 
    'Locations'    'Rainfall'    'Temperatures'

Exporting to the File

Use the xlswrite function to export a matrix to an Excel spreadsheet file. With xlswrite, you can export data from the workspace to any worksheet in the file, and to any location within that worksheet.

Inputs to xlswrite are:

Outputs from xlswrite are:

Example — Writing To an XLS File.   This example writes a mix of text and numeric data to the file tempdata.xls. Call xlswrite, specifying a worksheet labeled Temperatures, and the region within the worksheet where you want to write the data. The 4-by-2 matrix is written to the rectangular region that starts at cell E1 in its upper-left corner:

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

xlswrite('tempdata.xls', d, 'Temperatures', 'E1');

Adding a New Worksheet.   If the worksheet being written to does not already exist in the file, the MATLAB® software displays the following warning:

Warning: Added specified worksheet.

You can disable these warnings with this command:

warning off MATLAB:xlswrite:AddSheet

Importing from the File

Use xlsread to import a matrix from an Excel spreadsheet file into the MATLAB workspace. You can import data from any worksheet in the file, and from any location within that worksheet. You also optionally can have xlsread open an Excel window showing the file, and then interactively select the worksheet and range of data to be read by the function.

Inputs to xlsread are:

Three separate outputs from xlsread are:

Example — Reading from an XLS File.   Continuing with the previous example, to import only the numeric data, use xlsread with a single return argument. 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 numeric data and text data, specify two return values for xlsread:

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

headertext = 
    'Time'    'Temp'

ndata =
    12    98
    13    99
    14    97

Lotus 123 Spreadsheets

This section covers

For more detailed information and examples, see the wk1finfo, wk1write, and wk1read reference pages.

Getting Information About the File

Use the wk1finfo function to determine if a file contains a Lotus WK1 spreadsheet.

Inputs to wk1finfo are:

Outputs from wk1finfo are:

Example — Querying a WK1 File.   This example returns information about spreadsheet file matA.wk1:

[extens, type] = wk1finfo('matA.wk1')

extens =
   WK1
type =
   Lotus 123 Spreadsheet

Exporting to the File

Use the wk1write function to export a matrix to a Lotus spreadsheet file. You have the choice of positioning the matrix starting at the first row and column of the spreadsheet, or at any other location in the file.

To export to a specific location in the file, use the second syntax, indicating a zero-based starting row and column.

Inputs to wk1write are:

Example — Writing to a WK1 File.   This example exports an 8-by-8 matrix to spreadsheet file matA.wk1:

A = [1:8; 11:18; 21:28; 31:38; 41:48; 51:58; 61:68; 71:78];
A =
     1     2     3     4     5     6     7     8
    11    12    13    14    15    16    17    18
    21    22    23    24    25    26    27    28
    31    32    33    34    35    36    37    38
    41    42    43    44    45    46    47    48
    51    52    53    54    55    56    57    58
    61    62    63    64    65    66    67    68
    71    72    73    74    75    76    77    78

wk1write('matA.wk1', A);

Importing from the File

To import data from the spreadsheet into the MATLAB workspace, use wk1read. There are three ways to call wk1read. The first two shown here are similar to wk1write. The third enables you to select a range of values from the spreadsheet. You can specify the range argument with a one-based vector, spreadsheet notation (e.g., 'A1..B7'), or using a named range (e.g., 'Sales').

Inputs to wk1read are:

Outputs from wk1read are:

Example — Reading from a WK1 File.   Read in a limited block of the spreadsheet data by specifying the upper-left row and column of the block using zero-based indexing:

M = wk1read('matA.wk1', 3, 2)
M =
    33    34    35    36    37    38
    43    44    45    46    47    48
    53    54    55    56    57    58
    63    64    65    66    67    68
    73    74    75    76    77    78
  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS