Thread Subject: Importing selected column from a Csv files

Subject: Importing selected column from a Csv files

From: Luca Cerone

Date: 11 Dec, 2007 12:40:53

Message: 1 of 3

Hi,
I have a very big CSV data archive.
Due to dimensions I'm not able to import all of it in the
Workspace.

As I don't need all the variables,
I just would like to read a group of selected columns.

Let's say I want to import columns 1 39 50 90.
I could do it with a cicle using "csvread" and importing one
column once a time, but I would prefer doing this with a
built in function (something like csvread(file, [1 39 50
90]). Is this possible???

Thank you very much in advance!
Best regards -Luca-

Subject: Importing selected column from a Csv files

From: William Dampier

Date: 12 Dec, 2007 20:12:46

Message: 2 of 3

"Luca Cerone" <cerone@to-see.it> wrote in message
<fjm0gl$s1u$1@fred.mathworks.com>...
> Hi,
> I have a very big CSV data archive.
> Due to dimensions I'm not able to import all of it in the
> Workspace.
>
> As I don't need all the variables,
> I just would like to read a group of selected columns.
>
> Let's say I want to import columns 1 39 50 90.
> I could do it with a cicle using "csvread" and importing one
> column once a time, but I would prefer doing this with a
> built in function (something like csvread(file, [1 39 50
> 90]). Is this possible???
>
> Thank you very much in advance!
> Best regards -Luca-


This is a good case for using textscan.

Something like:

wanted_cols=[1 39 50 90];
format=[];
for I=1:num_cols
if any(I==wanted_cols)
format=[format '%n'];
else
format=[format '%*n'];
end
end

(There may be a more elegant way to create this format but
I've never been able to figure it out.)

Now we have a char array which we can use as the 'format' in
textscan.

Now to read the file:
fid=fopen('filename.csv','rt');

data=textscan(fid,format,'delimiter',',')

fclose(fid);



In my experience this method tends to be faster than using a
loop and csvread for large files since it only scans the
file once.

Hope this helps!

Subject: Importing selected column from a Csv files

From: Luca Cerone

Date: 17 Dec, 2007 09:41:36

Message: 3 of 3

Thank you very much, it seems a smart idea!
(I do something similar to create MySql queries)

Thanks a lot!

"William Dampier" <walldo2@gmail.com> wrote in message
<fjpfbu$368$1@fred.mathworks.com>...
> "Luca Cerone" <cerone@to-see.it> wrote in message
> <fjm0gl$s1u$1@fred.mathworks.com>...
> > Hi,
> > I have a very big CSV data archive.
> > Due to dimensions I'm not able to import all of it in the
> > Workspace.
> >
> > As I don't need all the variables,
> > I just would like to read a group of selected columns.
> >
> > Let's say I want to import columns 1 39 50 90.
> > I could do it with a cicle using "csvread" and importing one
> > column once a time, but I would prefer doing this with a
> > built in function (something like csvread(file, [1 39 50
> > 90]). Is this possible???
> >
> > Thank you very much in advance!
> > Best regards -Luca-
>
>
> This is a good case for using textscan.
>
> Something like:
>
> wanted_cols=[1 39 50 90];
> format=[];
> for I=1:num_cols
> if any(I==wanted_cols)
> format=[format '%n'];
> else
> format=[format '%*n'];
> end
> end
>
> (There may be a more elegant way to create this format but
> I've never been able to figure it out.)
>
> Now we have a char array which we can use as the 'format' in
> textscan.
>
> Now to read the file:
> fid=fopen('filename.csv','rt');
>
> data=textscan(fid,format,'delimiter',',')
>
> fclose(fid);
>
>
>
> In my experience this method tends to be faster than using a
> loop and csvread for large files since it only scans the
> file once.
>
> Hope this helps!

Tags for this Thread

Everyone's Tags:

read(2), csv read(2), csv, import

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
csv read Matthew Simoneau 12 Dec, 2007 15:29:54
read Matthew Simoneau 12 Dec, 2007 15:29:50
import Luca Cerone 11 Dec, 2007 07:45:13
read Luca Cerone 11 Dec, 2007 07:45:13
csv read Luca Cerone 11 Dec, 2007 07:45:13
csv Luca Cerone 11 Dec, 2007 07:45:13
rssFeed for this Thread

Public Submission Policy

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 Disclaimer prior to use.

Contact us at files@mathworks.com