MATLAB reads Excel and TXT file functions

Source: Internet
Author: User

Note that MATLAB does not recognize Chinese characters. The most difficult Chinese characters are included in the read/write files.

Excel reading function xlsread

Text reading function csvread

Xlsread get data and text from a spreadsheet in an Excel Workbook.
[Numeric, txt, raw] = xlsread (File) reads the data specified in the Excel
File, file. The numeric cells in file are returned in numeric, the text
Cells in file are returned in txt, while the raw, unprocessed Cell
Content is returned in raw.
 
[Numeric, txt, raw] = xlsread (file, sheet, range) reads the Data Specified
In range from the worksheet sheet, in the Excel file specified in file.
It is possible to select the range of data interactively (see examples
Below). Please note that the full functionality of xlsread depends on
The ability to start excel as a COM server from MATLAB.
 
[Numeric, txt, raw] = xlsread (file, sheet, range, 'Basic ') reads an xls file
Above, using basic input mode. This is the mode used on UNIX platforms
As well as on windows when Excel is not available as a COM server.
In this mode, xlsread does not use Excel as a COM server, which limits
Import ability. Without excel as a COM server, range will be ignored
And, consequently, the whole Active range of a sheet will be imported.
Also, in basic mode, sheet is case-sensitive and must be a string.
 
[Numeric, txt, raw] = xlsread (file, sheet, range, '', customfun)
[Numeric, txt, raw, customoutput] = xlsread (file, sheet, range, '', customfun)
When the Excel COM server is used, allows passing in a handle to
Custom function. This function will be called just before retrieving
The actual data from Excel. It must take an Excel range object (e.g.
Type 'interface. microsoft_excel_5.0_object_library.range ') as input,
And return one as output. Optionally, this custom function may return
A second output argument, which will be returned from xlsread as
Fourth output argument, customoutput. For details of what is possible
Using the Excel COM interface, please refer to Microsoft documentation.
 
Input parameters:
File: String defining the file to read from. default directory is PWD.
Default extension is 'xls '.
Sheet: String defining worksheet name in workbook file.
Double scalar defining worksheet index in workbook file. See
Note 1.
Range: String defining the data range in a worksheet. See note 2.
Mode: String enforcing basic import mode. Valid value = 'basic '. This
Is the mode always used when com is not available (e.g. On UNIX ).
 
Return parameters:
Numeric = N x m array of type double.
TXT = r x s cell string array containing text cells in range.
Raw = v x w cell array containing unprocessed numeric and text data.
Both numeric and TXT are subsets of raw.
 
Examples:
1. Default operation:
Numeric = xlsread (File );
[Numeric, TXT] = xlsread (File );
[Numeric, txt, raw] = xlsread (File );
 
2. Get data from the default region:
Numeric = xlsread ('C:/Matlab/work/myspreadsheet ')
 
3. Get data from the used area in a sheet other than the first sheet:
Numeric = xlsread ('C:/Matlab/work/myspreadsheet ', 'sheet2 ')
 
4. Get data from a named sheet:
Numeric = xlsread ('C:/Matlab/work/myspreadsheet ', 'nbdata ')
 
5. Get data from a specified region in a sheet other than the first
Sheet:
Numeric = xlsread ('C:/Matlab/work/myspreadsheet ', 'sheet2', 'a2: j5 ')

6. Get data from a specified region in a named sheet:
Numeric = xlsread ('C:/Matlab/work/myspreadsheet ', 'nbdata', 'a2: j5 ')

7. get data from a region in a sheet specified by index:
Numeric = xlsread ('C:/Matlab/work/myspreadsheet ', 2, 'a2: j5 ')

8. Interactive region selection:
Numeric = xlsread ('C:/Matlab/work/myspreadsheet ',-1 );
You have to select the active region and the active sheet in
Excel window that will come into focus. Click OK in the data
Selection dialog when you have finished selecting the active region.
 
9. Using the custom function:
[Numeric, txt, raw, customoutput] = xlsread('equity.xls ',..., @ mycustomfun)
Where the customfun is defined:
 
Function [datarange, customoutput] = mycustomfun (datarange)
Datarange. numberformat = 'date ';
Customoutput = 'Anything I want ';

This will convert to dates all cells where that is possible.
 
NOTE 1: The first worksheet of the workbook is the default sheet. If
Sheet is-1, Excel comes to the foreground to enable interactive
Selection (Optional). In interactive mode, a dialogue will prompt
You to click the OK button in that dialogue to continue in MATLAB.
(Only supported when Excel COM server is available .)
Note 2: The regular form is: 'd2: F3 'to select Rectangular Region D2: F3
In a worksheet. range is not case sensitive and uses Excel A1
Notation (see Excel help). (Only supported when Excel COM Server
Is available .)
NOTE 3: EXCEL formats other than the default can also be read.
(Only supported when Excel COM server is available .)
 
See also xlswrite, csvread, csvwrite, dlmread, dlmwrite, textscan.

Reference page in help Browser
Doc xlsread

Csvread read a comma separated value file.
M = csvread ('filename') reads a comma separated value formatted File
Filename. The result is returned in M. The file can only contain
Numeric values.
 
M = csvread ('filename', R, c) reads data from the comma separated value
Formatted file starting at row R and column C. R and C are zero-
Based so that r = 0 and c = 0 specifies the first value in the file.
 
M = csvread ('filename', R, C, RNG) reads only the range specified
By RNG = [R1 C1 R2 C2] Where (R1, C1) is the upper-left corner
The data to be read and (R2, C2) is the lower-right corner. RNG
Can also be specified using spreadsheet notation as in RNG = 'a1... B7 '.
 
Csvread fills empty delimited fields with zero. Data Files where
The lines end with a comma will produce a result with an extra last
Column Filled with zeros.
 
See also csvwrite, dlmread, dlmwrite, load, fileformats, textscan.

Reference page in help Browser
Doc csvread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.