Programming |
Working with Spreadsheets
You can use MATLAB to import and export data to the following types of spreadsheets:
Microsoft Excel Spreadsheets
See the xlsfinfo
, xlswrite
, and xlsread
reference pages for more detailed information and examples.
Getting Information About the File
Use the xlsfinfo
function to determine if a file contains a readable Microsoft Excel spreadsheet.
'Microsoft Excel Spreadsheet'
if the file contains an Excel worksheet readable with the xlsread
function. Otherwise, it contains an empty string (''
).
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.
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 to write the data to. 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, MATLAB displays the following warning:
You can disable these warnings with the command
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 can also optionally 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.
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:
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
Exporting Audio/Video Data | Lotus 123 Spreadsheets |
© 1994-2005 The MathWorks, Inc.