Anda di halaman 1dari 4

Pasting Excel Data from the Clipboard Paste Excel data from the clipboard into MATLAB using

one of the following metho ds: Select Edit > Paste to Workspace. Call uiimport -pastespecial. Open an existing variable in the MATLAB Variable Editor, and select Edit > Paste Excel Data. Back to Top Selecting the Spreadsheet and Variables Interactively The Import Wizard is a graphical user interface that helps you find a file, spec ify a worksheet, and define the variables to use in the workspace. For worksheet s with row or column headers, the Import Wizard allows you to create variables b ased on the rows or columns. However, you cannot specify a range of data to impo rt. To start the Import Wizard, use one of the following methods: Select File > Import Data. Double-click a file name in the Current Folder browser. Call uiimport. Back to Top Reading a Specific Worksheet and Range of Data Consider the file climate.xls created with xlswrite as follows: d = {'Time', 'Temp'; 12 98; 13 99; 14 97} xlswrite('climate.xls', d, 'Temperatures'); To import the numeric data into a matrix, use xlsread with a single return argum ent. xlsread ignores any leading row or column of text in the numeric result: ndata = xlsread('climate.xls', 'Temperatures') ndata = 12 13 14 To import : 98 99 97 both numeric data and text data, specify two return values for xlsread

[ndata, headertext] = xlsread('climate.xls', 'Temperatures') ndata = 12 13 14 98 99 97

headertext = 'Time' 'Temp' To read only the first row of data, specify the range: firstrow = xlsread('climate.xls', 'Temperatures', 'A2:B2') firstrow = 12 98 Getting Information about a To determine whether a file nfo function . For readable Spreadsheet'. Otherwise, it

Spreadsheet contains a readable Excel spreadsheet, use the xlsfi files, xlsfinfo returns the string 'Microsoft Excel returns an empty string ('').

You also can use xlsfinfo to identify the names of the worksheets in the file, a nd to obtain the file format reported by Excel. For example, retrieve informatio n on the spreadsheet climate.xls: [type, sheets] = xlsfinfo('climate.xls') type = Microsoft Excel Spreadsheet sheets = 'Sheet1' 'Sheet2' 'Sheet3' Back to Top

'Temperatures'

Reading All Worksheets into a Structure The importdata function reads data from an Excel file into a structure. Continui ng the example in Reading a Specific Worksheet and Range of Data, where the data includes column headers, a call of the form climate = importdata('climate.xls') returns the nested structure array climate = data: [1x1 struct] textdata: [1x1 struct] colheaders: [1x1 struct] Structures created from Excel files with row headers include the field rowheader s, which also contains a 1-by-1 structure. The structure named data contains one field for each worksheet with numeric data . The other structures contain one field for each worksheet with text cells or h eaders. In this case: climate.data = Temperatures: [3x2 double] climate.textdata = Temperatures: {'Time' 'Temp'} climate.colheaders = Temperatures: {'Time' 'Temp'} If the Excel file contains only numeric data (no row or column headers, and no i nner cells with text), the output structure is simpler. importdata returns a 1-b y-1 structure, with one field for each worksheet with data. For example, if the Temperatures worksheet in climate_nums.xls does not include column headers, the call ndata = importdata('climate_nums.xls') % only numeric data % with column headers

returns ndata = Temperatures: [3x2 double] Back to Top Excel Version Support If your system has Excel for Windows installed, including the COM server (part o f the typical installation of Excel): All MATLAB import options support XLS, XLSX, XLSB, and XLSM formats. xlsread als o imports HTML-based formats. If you have Excel 2003 installed, but want to read a 2007 format (such as XLSX, XLSB, or XLSM), install the Office 2007 Compatibility Pack. xlsread includes an option to open Excel and select the range of data interactiv ely. To use this option, call xlsread with the following syntax: mydata = xlsread(filename, -1) Restrictions If your system does not have Excel for Windows installed, or the COM server is n ot available: The Import Wizard, importdata, and xlsread only read XLS files compatible with E xcel 97-2003. You can specify a worksheet to read in the Excel file with the xlsread function, but you cannot specify a range of data. See the xlsread reference page for addi tional information. Note Large files in XLSX format sometimes load slowly. For better import and e xport performance with Excel 2007 files, Microsoft recommends that you use the X LSB format. Back to Top Converting Dates In both MATLAB and Excel applications, dates can be represented as character str ings or numeric values. For example, May 31, 2009, can be represented as the cha racter string '05/31/09' or as the numeric value 733924. Within MATLAB, the date str and datenum functions allow you to convert easily between string and numeric representations. If you import a spreadsheet with dates stored as strings on a system with Excel for Windows, you do not need to convert the dates before processing in MATLAB. However, if you import a spreadsheet with dates stored as numbers, or if your sy stem does not have Excel for Windows, you must convert the dates. Both Excel and MATLAB represent numeric dates as a number of serial days elapsed from a specif ic reference date, but the applications use different reference dates. The following table lists the reference dates for MATLAB and Excel. For more inf ormation on the 1900 and 1904 date systems, see the Excel help. Application Reference Date MATLAB January 0, 0000 Excel for Windows January 1, 1900 Excel for the Macintosh January 2, 1904

Example Importing an Excel File with Numeric Dates Consider the hypothetical file weight_log.xls with Date Weight 10/31/96 174.8 11/29/96 179.3 12/30/96 190.4 01/31/97 185.7 To import this file, first convert the dates within Excel to a numeric format. I n Windows, the file now appears as Date 35369 35398 35429 35461 Import the Weight 174.8 175.3 190.4 185.7 file:

wt = xlsread('weight_log.xls'); Convert the dates to the MATLAB reference date. If the file uses the 1900 date s ystem (the default in Excel for Windows): datecol = 1; wt(:,datecol) = wt(:,datecol) + datenum('30-Dec-1899'); If the file uses the 1904 date system (the default in Excel for the Macintosh): datecol = 1; wt(:,datecol) = wt(:,datecol) + datenum('01-Jan-1904'); Back to Top Was this topic helpful? Importing XML Documents Importing Scientific Data Files

Recommended Products MATLAB Signal Processing Toolbox Optimization Toolbox Statistics Toolbox Control System Toolbox Get MATLAB trial software Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts. Get the Interactive Kit

Anda mungkin juga menyukai