How to use textscan to properly read a csv file?

82 views (last 30 days)
Zak Kessel
Zak Kessel on 8 Jun 2016
Edited: dpb on 8 Jun 2016
Hi everybody, I'm having a problem with trying to read a csv file. The file is 18 columns by 234 rows. The first 34 rows are either strings or blank, and I don't need them. I am interested in 4 specific columns filled with decimal numbers, as I need to take that data and make some calculations and plot it. But I can't seem to get over the initial step of correctly putting those 4 specific columns into an array or saving them as variables. Any help? Sorry if my question is confusing.
An example file is now attached.
  5 Comments
dpb
dpb on 8 Jun 2016
Edited: dpb on 8 Jun 2016
Well, to skip a column you really don't care particularly whether it's numeric or character so to read numeric in the above columns it's
  1. Read a number
  2. Skip six columns, read two numbers
  3. Skip six columns, read a number
  4. Skip remainder of line (or two columns)
By analogy,
fmt=['%f' repmat('%*s',1,6)'%f %f' repmat('%*s',1,6)'%f' '%*s %*s'];
Your desired columns didn't lead to much symmetry so it's fairly bulky but still straightforward.
Zak Kessel
Zak Kessel on 8 Jun 2016
Edited: dpb on 8 Jun 2016
fileID = fopen('C:\Users\zk04478\Desktop\QuestionExample.csv')
data=cell2mat(textscan(fileID,'','delimiter',',','headerlines',34,'collectoutput',3))
fmt=['%f' repmat('%*s',1,6) '%f' '%f' repmat('%*s',1,6) '%f' '%*s' '%*s'];
This is my code above so far, and the resulting command window and value for "data" when I run it. Still unsure why it doesn't seem to be skipping the headers or the columns that I need. I'm very sorry, but I appreciate the help!

Sign in to comment.

Answers (1)

dpb
dpb on 8 Jun 2016
Well, you don't say which columns, but it's simple enough...and actually, if after the header/text lines it is all numeric then just use csvread --
data=csvread('yourfile.csv',34); % read all the numeric data
idx=[2 7 12 13]; % arbitrary for columns
data=data(:,idx); % save those for use
With textscan the above would be almost the same (after the fopen preamble, of course)
data=cell2mat(textscan(fid,'','delimiter',',','headerlines',34,'collectoutput',1));
NB: I went ahead and wrapped the textscan call in cell2mat to return a double array directly instead of the cell array one otherwise gets from textscan
  4 Comments
Zak Kessel
Zak Kessel on 8 Jun 2016
I attempted this code below:
fileID = fopen('C:\Users\zk04478\Desktop\QuestionExample.csv');
fmt=['%f' repmat('%s',1,6) '%f' '%f' repmat('%s',1,6) '%f' '%*s' '%*s'];
data= cell2mat(textscan(fileID, fmt,'delimiter',',','headerlines',34,'collectoutput',1))
And it gave me this error:
Error using cell2mat (line 45) All contents of the input cell array must be of the same data type.
Error in test3 (line 4) data=cell2mat(textscan(fileID, fmt,'delimiter',',','headerlines',34,'collectoutput',1))
dpb
dpb on 8 Jun 2016
Edited: dpb on 8 Jun 2016
Missing the '*' on some (if not all) of the '%s' fields to skip them...oh, I see I missed at least one initially, but will caution the user needs to carefully read and understand the posted code as well... :)
>> fmt=['%f' repmat('%*s',1,6) '%f %f' repmat('%*s',1,6) '%f %*s %*s']
fmt =
%f%*s%*s%*s%*s%*s%*s%f %f%*s%*s%*s%*s%*s%*s%f %*s %*s
>> fid=fopen('zak.csv')
fid =
4
>> cell2mat(textscan(fid,fmt,'delimiter',',','headerlines',34','collectoutput',1))
ans =
0 0 0 3
1 0 0 3
2 0 0 3
3 0 0 3
4 0 0 3
5 0 0 3
6 0 0 3
7 0 0 3
8 0 0 3
9 0 0 3
10 0 0 3
11 0 0 3
12 0 0 3
>> fid=fclose(fid);
>>
QED :)

Sign in to comment.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!