Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Data extraction from CSV file

Asked by Chandan on 4 Dec 2012

I have numerous csv files generated from various tests that I have conducted. The issue is that these csv files contain strings as well as numbers. I want to extract an entire column from this file.

I have looked at several solutions but nothing seems to work and also I am new to MATLAB. So can someone kindly take a look at it.

Chandan

Tags

Products

No products are associated with this question.

3 Answers

Answer by Sean de Wolski on 4 Dec 2012

Are you using R2012b?

If so - use the Import Wizard. Click "Import Data" on the home tab and if the *.csv file has any structure, it will parse it for you.

4 Comments

Sean de Wolski on 4 Dec 2012

If your company/university has SMS you should be able to upgrade at no cost to you. Talk to your IT people.

The new tool also allows you to generate code for a *.csv file etc. It's pretty cool.

per isakson on 4 Dec 2012

R2012a has "Import data ..." in the File menu. Is that tool more powerful in R2012b?

Sean de Wolski on 5 Dec 2012

@Per, in 12b it can handle text files and weirder things that are not well defined.

Sean de Wolski
Answer by Image Analyst on 4 Dec 2012

There seems to be no capability to skip rows in csvread() or dlmread() unless the skipped rows have the same format as the numerical table below the rows. So it appears that it can't handle skipping header lines with arbitrary strings on them. This would be a nice, and I think an easy to add, upgrade that many people have been wanting for a long long time. If you want, you could easily write your own to get certain lines, write them out to a temporary file, call csvread on that temporary file, then delete the temporary file. In the meantime you may have to read lines yourself with fgetl() and parse with textscan() or sscanf().

3 Comments

Chandan on 4 Dec 2012

The thing is since its many files, doing that would be hectic. I am trying to extract the entire column which goes from numbers 33.XXX, 57.XXX, 61.XXX, 61.XXX and so on.

Image Analyst on 4 Dec 2012

It's not that hard, but if you want code that does it, just check the File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/index?utf8=%E2%9C%93&term=csvread

Image Analyst on 5 Dec 2012

Actually it looks like Sean's right. In R2012b it seems like importdata has the header-line-skipping capability that I was wanting for csvread and dlmread. I haven't tried it but that's just based on reading the help.

Image Analyst
Answer by per isakson on 4 Dec 2012
Edited by per isakson on 4 Dec 2012
  • your file has something like 18 headerlines
  • the data rows have a zillions fields, many of which are empty
  • the rows have both "text" and "numeric" fields
  • the file has a footer with text and a summary row
  • the files has many thousand rows

I would say that you must read the file in two steps

  • determine number of rows of header, data and footer
  • read the file with textscan.

The format string "must" be generated automatically, which can be done in the first step. Reading the file twice has a minor effect on the performance if it fits in the system cache.

I guess that few if any tools in the File Exchange handles footers

0 Comments

per isakson

Contact us