How to select one column of data from a csv file containing headers and strings?

4 views (last 30 days)
The csv files in question each contain five columns, and an unknown number of rows:
Suppose a file has N rows. I want to select all the data from B2 to BN and save them into a table.
I tried this:
table_x = csvread('filename.csv', 1, 1, [1, 1, Inf, 1]);
That doesn't work. It wants me specify the number of rows, which I don't know. I guess can't use csvread because I don't know what N is and because the file contains non-numeric data.

Accepted Answer

Peter Perkins
Peter Perkins on 28 Jul 2015
It's not clear what you mean by, "save them into a table". It sound like you mean, "save that one column to a numeric variable". In R2013b or later, use readtable:
t = readtable('filename.csv')
x = t.X_mg_ % readtable will have modified X(mg)

More Answers (1)

Walter Roberson
Walter Roberson on 28 Jul 2015
fmt = '%*f%f%*f%*f%*[\n]';
fid = fopen('filename.csv', 'rt');
datacell = textscan(fid, fmt, 'Delimiter', ',', 'HeaderLines', 1);
fclose(fid);
table_x = datacell{1};
  3 Comments
Stephen23
Stephen23 on 28 Jul 2015
Edited: Stephen23 on 28 Jul 2015
Can you please upload the datafile: edit your question, click on the paperclip icon, and then push both the Choose file and Attach file buttons.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!