5.0

5.0 | 2 ratings Rate this file 68 downloads (last 30 days) File Size: 4.48 KB File ID: #23573

CSVIMPORT

by Ashish Sadanandan

 

06 Apr 2009 (Updated 28 Apr 2009)

Code covered by the BSD License  

CSVIMPORT reads the specified CSV file and stores the contents in a cell array or matrix.

Download Now | Watch this File

File Information
Description

The file can contain any combination of text & numeric values. Output data format will vary depending on the exact composition of the file data.

CSVIMPORT( fileName ) : fileName - String specifying the CSV file to be read. Set to [] to interactively select the file.

CSVIMPORT( fileName, ... ) : Specify a list of options to be applied when importing the CSV file.
                    The possible options are:

                    delimiter - String to be used as column delimiter. Default value is , (comma)

                    columns - String or cell array of string listing the columns from which data is to be extracted. If omitted data from all columns in the file is imported.

                    outputAsChar - true / false value indicating whether the data should be output as characters. If set to false the function attempts to convert each column into a numeric array, it outputs the column as characters if conversion of any data element in the column fails. Default value is false.

                    uniformOutput - true / false value indicating whether output can be returned without encapsulation in a cell array. This parameter is ignored if the columns / table cannot be converted into a matrix.

                    noHeader - true / false value indicating whether the CSV file's first line contains column headings. Default value is false.

                    ignoreWSpace - true / false value indicating whether to ignore leading and trailing whitespace in the column headers; ignored if noHeader is set to true. Default value is false.

The parameters must be sepcified in the form of param-value pairs, parameter names are not case-sensitive and partial matching is supported.

[C1 C2 C3] = CSVIMPORT( fileName, 'columns', {'C1', 'C2', C3'}, ... )
  This form returns the data from columns in output variables C1, C2 and C3 respectively, the
  column names are case-sensitive and must match a column name in the file exactly. When fetching
  data in column mode the number of output columns must match the number of columns to read or it
  must be one. In the latter case the data from the columns is returned as a single cell matrix.

[C1 C2 C3] = CSVIMPORT( fileName, 'columns', [2, 3, 4], ,'noHeader', true, ... )
  This form returns the data from columns in output variables C1, C2 and C3 respectively, the
  columns parameter must contain the column indices when the 'noHeader' option is set to true.

Notes: 1. Function has not been tested on badly formatted CSV files.
            2. Created using R2007b but has been tested on R2006b.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
24 Aug 2009 vadim  
24 Aug 2009 vadim

Great job!

27 Oct 2009 maney

Hi Ashish,
 I ran your code and I am getting the following error:

??? Undefined function or variable 'isscalar'.

please advise,
Thanks - maney

28 Oct 2009 Ashish Sadanandan

Maney,
Try running 'which -all isscalar' at the command line, if MATLAB doesn't find any function by that name then you're either using an old version which doesn't have this function or you have problems with the MATLAB search path. Let's assume it's the former since you'd probably be having other errors too if the cause were to be the latter.

ISSCALAR simply checks whether the operand is a 1x1 matrix. You can replace isscalar(x) with (numel(x) == 1) OR (prod(size(x)) == 1)

HTH,
Ashish

18 Nov 2009 Gordon

Hi

Is it possible to use this code, or anything else for a csv file available on the web, ie:

http://www.google.com/insights/search/overviewReport?q=matlab&geo=US&cmpt=q&content=1&export=2

thanks

14 Dec 2009 Gordon

This only worked on ascii files, but combined with unicode2ascii
it did a fantastic job.

Please login to add a comment or rating.
Updates
28 Apr 2009

Corrected typo in an error message
Added igonoreWSpace option

Tag Activity for this File
Tag Applied By Date/Time
csv Ashish Sadanandan 01 May 2009 00:22:08
csvread Ashish Sadanandan 01 May 2009 00:22:08
read csv Ashish Sadanandan 01 May 2009 00:22:25
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com