| MATLAB® | ![]() |
| On this page… |
|---|
This section covers
For more detailed information and examples, see the xlsfinfo, xlswrite, and xlsread reference pages.
Use the xlsfinfo function to determine if a file contains a readable Microsoft® Excel® spreadsheet.
Inputs to xlsfinfo are:
Name of the spreadsheet file
Outputs from xlsfinfo are:
String 'Microsoft Excel Spreadsheet' if the file contains an Excel® worksheet readable with the xlsread function. Otherwise, it contains an empty string ('').
Cell array of strings containing the names of each worksheet in the file.
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'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:
Name of the spreadsheet file
Matrix to be exported
Name of the worksheet to receive the data
Range of cells on the worksheet in which to write the data
Outputs from xlswrite are:
Pass or fail status
Any warning or error message generated along with its message identifier
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
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:
Name of the spreadsheet file
Matrix to be imported
Name of the worksheet from which to read the data
Range of cells on the worksheet from which to read the data
Keyword that opens an Excel window, enabling you to interactively select the worksheet and range of data to read
Keyword that imports using basic import mode
Three separate outputs from xlsread are:
Numeric data
String data
Any unprocessed cell content
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 97To 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 97This section covers
For more detailed information and examples, see the wk1finfo, wk1write, and wk1read reference pages.
Use the wk1finfo function to determine if a file contains a Lotus WK1 spreadsheet.
Inputs to wk1finfo are:
Name of the spreadsheet file
Outputs from wk1finfo are:
String 'WK1' if the file is a Lotus spreadsheet readable with the wk1read function. Otherwise, it contains an empty string ('').
String 'Lotus 123 Spreadsheet'
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
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:
Name of the spreadsheet file
Matrix to be exported
Location in the file in which to write the data
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);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:
Name of the spreadsheet file
Spreadsheet location from which to read the data
Range of cells from which to read the data
Outputs from wk1read are:
Requested data from the spreadsheet
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
![]() | Working with Audio and Video Data | Using Low-Level File I/O Functions | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |