File Exchange

image thumbnail

CSVIMPORT

version 1.4 (4.59 KB) by

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

4.86667
34 Ratings

192 Downloads

Updated

View License

  CSVIMPORT reads the specified CSV file and stores the contents in a cell array or matrix
 
  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 strings listing the columns
                    from which data is to be extracted. If omitted data
                    from all columns in the file is imported. If file
                    does not contain a header row, the columns
                    parameter can be a numeric array listing column
                    indices from which data is to be extracted.
                    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 specified 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', [1, 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.

Comments and Ratings (57)

Vinh Tu

LI LI

LI LI (view profile)

thx for sharing. It's working pretty good.

Absolutely fantastic

GUO GUOQIANG

Hi,
how do i plot the data after i have imported them into matlab?
the matlab saved it in as 'ans' in the workspace
Please help,
i am relatively new to matlab.
Thank you

@Steven Cardinali:

Your fix works for importing data with no other arguments. But if you want to specify specific columns, you need to change the code to the following to make it work as intended:

if ~isempty(p.columns)
    nLines = numel( find( data == sprintf( '\n' ) ) ) + 1;
else
    nLines = numel( find( data == sprintf( '\n' ) ) ) + 2;
end

I'll eventually re-write this entire function to make this cleaner...

Any feature requests or suggestions?

Catherine

I have the same problem as Loretta as well.

Loretta

Hi,
I want to open a csv file and store it in array:
My file csv is:
01/12/2016 15:21:28.600, -9.474
01/12/2016 15:21:28.800, -9.569
01/12/2016 15:21:29.000, -9.610
01/12/2016 15:21:29.200, -9.534
01/12/2016 15:21:29.400, -9.499
01/12/2016 15:21:29.600, -9.486
01/12/2016 15:21:29.800, -9.491
01/12/2016 15:21:30.000, -9.515
I want to save the third colum (after comma) to an array:
my_array=
-9.474
-9.569
-9.610
.....
Can your function do this?
I’m new to matlab…I tryed:

csvimport('myfile.csv')
I get <8 x2 cell>

How can I store only the third column (numbers) in an array ?
many thanks

Awesome code. But doesn't import the last line for me. I went into the file and changed line 233 to "nLines = numel( find( data == sprintf( '\n' ) ) ) + 2;" from "nLines = numel( find( data == sprintf( '\n' ) ) ) + 1;"... i.e. just changed the 1 to a 2 and it fixed the problem.

Ashish Sadanandan

@SK

Sorry, but that's unlikely to happen. I no longer have access to MATLAB, and in any case, going down the rabbit hole of handling delimiters escaped in various ways is not something I'm keen on doing.

SK

SK (view profile)

There is an issue you may want to fix. Commas inside a quoted value should be ignored.

@Anish Sadanandan I ended up using something else because it just took way too long in the end for huge CSV's, but thanks anyway! It worked very well for smaller CSV's, thank you!!

Ashish Sadanandan

@Sagarika Kadambi

There is no size limitation built into the function, but I've never tested it with files that large. You could try setting the 'outputAsChar' option to true, which should reduce memory requirements. Or try playing with a few different file sizes, for e.g. reduce to 450,000 lines, if it works, try 700,000, if it doesn't 300,000 and so.

Sorry I don't have a definite answer for you.

Hi,
Does this work for very large csv files? I'm trying to use one that is about 900,000 lines, and the function simply doesn't work..

I'm using
[C2 C3] = csvimport('testCSV.csv','columns',2:3,'noHeader',true)

and it works for me and prints out something like:

C1 =
234234
364565
4744562
234123
235346
142314

C2 =
453466
234636
86789
878
234265
124346

which is what I want... but if I do it with the bigger csv file, it just prints out a list of numbers like:

234234
23425
53456
456457
456547

and so on..

my data looks something like this:

2014-6-06 08:03:19, 439105, 1053224, Front Entrance

    2014-6-06 09:43:21, 439105, 1696241, Main Exit

    2014-6-06 10:01:54, 1836139, 1593258, Back Archway

    2014-6-06 04:45:13, 1464748, 439105, Side Exit

Am I doing something wrong?

This is great. Thank you

Justin

Justin (view profile)

Simply Awesome. much improved over the woeful csvread function...

docsam

docsam (view profile)

Great work Ashish.. very useful, saved me a lot of h.assle

Michelle

Very nice! In case it's helpful to anybody else, I was able to import data with missing values directly without the need to convert afterwards by changing line 284 from

if ~any( cellfun( @isnan, colData ) )

to

if ~any( cellfun( @isnan, colData ) & ~strcmp(data(startRowIdx:end,ii),'#N/A') & ~strcmp(data(startRowIdx:end,ii),'#n/a') )

stijn

stijn (view profile)

Oscar

Oscar (view profile)

sir, if csvexport is kindly share it with me..i am facing a problem with csvwirte i.e Error using dlmwrite (line 112)
The input cell array cannot be converted to a matrix.

Adam Li

Adam Li (view profile)

Great code!

Only issue I really don't understand is why I have to save the file as a Windows CSV file in order for it to work. My code didn't work when the files were saved as only CSV; the column vectors came back empty. Do you think you would be able to fix this problem?

Thanks!

Ashish Sadanandan

@Fan
Try adding a newline to the end of the file

Fan

Fan (view profile)

Works, any idea why it does not import the last row of the data?

Poolla

Poolla (view profile)

Awesome, saves lot of otherwise work. Keep it up!

Ged Ridgway

Ged Ridgway (view profile)

@Marcos, I had a similar problem, which seems to be fixed by adding a carriage return at the end of the csv file (e.g. using the MATLAB editor).

Marcos

Marcos (view profile)

Fantastic function, but I am confused by it's behavior with one of the files I need to import.

The format is
# header1 header2 ..... headern
1 v1..............v2.................vn
2 v1..............v2.................vn
.
.
.
11 v1..............v2.................vn

The values are mixed between numeric and chars, but the function only imports the first 11 rows, including the header, so the entire bottom row that starts with 11 is missing.

If I set 'noHeader' to true, then I get all 11 rows with values. I would prefer to not have to do this, so can someone explain why this would be happening?

jason

jason (view profile)

Pretty cool function. However, It would be nice if it called the imported date by the orginal file name and not 'ans'

DBS

DBS (view profile)

Greg

Greg (view profile)

Added support for commas that are in between double quotes. so that they stay in one cell. First it replaces commas than are not in between quotes to semicolons, then splits by semicolons. Then removes the quotes. Starts at line 246 (the main for loop)

rowData = fgetl( fid );
      if isempty( rowData )
        emptyRowsIdx = [emptyRowsIdx(:); ii];
        continue
      end
      %*** added by GSS
      repIdx = regexp(rowData, '(?:^|,)(?:"((?:[^"]|"")*)"|([^",]*))');
      rowData(repIdx(2:end))=';';
      rowData = regexp(rowData,';','split');
      for j=1:length(rowData)
          if ~isempty(rowData{j}) && rowData{j}(1)=='"'
              rowData{j}(1)=[];
              rowData{j}(end)=[];
          end
      end
      %****end GSS
      nDataElems = numel( rowData );

Some of the entries in the first column of my data contain a comma that should not be interpreted as a delimiter.

For example, the first three rows (with header row) are:
"School","Section Discipline"
"ACS Cogham International HS, UK","Physics"
"Adelphi University","Mathematics"

Can this be accomodated easily?

Works great. Thanks you!

vlema

vlema (view profile)

Great function! It worked in a badly formatted csv file. thanks for sharing.

Chris

Chris (view profile)

@Bernoulli Lizard: As Warwick said, if you're a Mac user you need to change the csv to a Windows format. If you you have Perl, using this line in a terminal will do:
perl -p -i -e 's/\r/\n/g' *.csv

Good day Ashish

Why does the file convert the first colunm to char type?

Excellent CSV import function. Thank you.

Chris, what did you do to read more than the first row? I'm having the same problem.

Ashish Sadanandan

Hi Sayanta,
Call the function as follows:

[C1 C2 C3 C4 C5] = csvimport( 'path\to\file.csv', 'columns', [1:5], 'noHeader', true, 'delimiter', ';' )

Sayanta

Hello Asish,

Here is the data I want to parse / import with mat lab code. I try to
use your function

[C1] = CSVIMPORT( 'CPU.csv', 'columns',{} );

Here is my data

 Patrol7;/NT_CPU/CPU_0/CPUprcrInterruptsPerSec;04/04/2012;11:04:46;77

Patrol7;/NT_CPU/CPU_0/CPUprcrInterruptsPerSec;
04/04/2012;11:04:47;66.9016

Patrol7;/NT_CPU/CPU_0/CPUprcrInterruptsPerSec;
04/04/2012;11:04:47;67.0833

Patrol7;/NT_CPU/CPU_0/CPUprcrInterruptsPerSec;04/04/2012;11:04:47;66.1

Patrol7;/NT_CPU/CPU_0/CPUprcrInterruptsPerSec;
04/04/2012;11:04:48;66.3115

Patrol7;/NT_CPU/CPU_0/CPUprcrInterruptsPerSec;
04/04/2012;11:04:49;66.3934

Patrol7;/NT_CPU/CPU_0/CPUprcrInterruptsPerSec;
04/04/2012;11:04:49;86.8525

Patrol7;/NT_CPU/CPU_0/CPUprcrInterruptsPerSec;
04/04/2012;11:04:49;78.7288

Patrol7;/NT_CPU/CPU_0/CPUprcrInterruptsPerSec;
04/04/2012;11:04:50;66.4918

I'm getting all data in C1 cell. I want my data (Delimited) in C1 C2 C3 C4 C5 in 5 different cell.

Could you please tell me how can I resolve my problem

Thanks in advance
Sayanta

Ben

Ben (view profile)

You rock! Thank you. The best csv-import function out there.

Warwick

If your'e a Mac user - I found that I had to save the .csv file as a "Windows comma delimted csv" file beforehand. Then it works superbly.

Yuri K

Yuri K (view profile)

I get a figure window open during this function run at line 275. It appears that I had a string 'White(some information)' that probably runs function white. Anyway to prevent such behavior?

Cheol Hee

Very nice!!! But data replaced some numbers. Fantastic!!

Cheol Hee

Chris

Chris (view profile)

I got it figured out. Works great!

Chris

Chris (view profile)

It's only reading in the first row. Am I mixing up an argument? My .csv uses commas as the delimiter (obviously) but also has a 'return' to separate each row. Is this valid? Thanks.

Mais

Mais (view profile)

well done! best csv-import function, i found here!

Fantastic!

Gordon

Gordon (view profile)

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

Gordon

Gordon (view profile)

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

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

maney

maney (view profile)

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

??? Undefined function or variable 'isscalar'.

please advise,
Thanks - maney

vadim

vadim (view profile)

Great job!

vadim

vadim (view profile)

Updates

1.4

Replaced calls to str2num with str2double, the former uses eval leading to unwanted side effects if cells contain text with function names

1.1

Corrected typo in an error message
Added igonoreWSpace option

MATLAB Release
MATLAB 7.5 (R2007b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video