1

I have a CSV file with the following content:

Header line1
Space
Space
Space
,1,2,3,
1,81,82,83

And I am trying to read the data portion into a numeric matrix. Here is the code I have implemented, however I am having issues.

%To get the number of rows in the file
for i = 1:9
   headerline = fgetl(fid);
   headerline = strsplit(headerline,',')
end

fclose(fid);

fopen(fid);
% to get the data
C = textscan(fid,'%s','headerline',4,'EmptyValue',=Inf)
rowsize = size(C{1});
data = []

% to store data in matrix
for i = 1:rowsize
   data = [data, strsplit(C{1}{i},',')];
end

Can anybody recommend a better way to just read the whole file into a numeric matrix? Thanks!

CircAnalyzer
  • 610
  • 1
  • 9
  • 29

1 Answers1

1

All you really need is this;

fid = fopen('your.csv');
data = cell2mat(textscan(fid, '%f%f%f%f', 'Delimiter', ',', 'HeaderLines', 4));

You could also use csvread (https://www.mathworks.com/help/matlab/ref/csvread.html) if your csv just contains numeric values.

M = csvread(filename,R1,C1) reads data from the file starting at row offset R1 and column offset C1. For example, the offsets R1=0, C1=0 specify the first value in the file.

So in this case:

data = csvread('filename.csv', 4, 0)

0xMB
  • 871
  • 1
  • 8
  • 15
  • I tried exactly that same line of code and get the following error: 'Error using dlmread(line 138). Mismatch between file and format string. Trouble reading 'Numeric' field from file (row number 1, field number 1) ==> – CircAnalyzer Sep 03 '15 at 22:50
  • If your csv contains non-numeric values then you should use `textscan` (https://www.mathworks.com/help/matlab/ref/textscan.html) – 0xMB Sep 04 '15 at 00:55
  • Can you please tell me why my implementation of textscan is not working? – CircAnalyzer Sep 04 '15 at 01:52
  • what's the best way to handle the space before the first comma on the first line? Because the updated code doesn't return anything either. I feel like there needs to be a way to ell textscan how to handle the space and comma in the front. Also, the only reason i wrote code to calculate the number of rows was because the data files are 122x160 matrices and i cant put 120 delimiter identifiers in the input of textscan. I only put a sample of the file just to get a sense of the concept, but i guess i should have pointed that out from the start. – CircAnalyzer Sep 04 '15 at 16:33
  • With my code I get NaN for an empty element. You can use `[repmat('%f', 1, 122)]` as the format argument for textscan. What version of Matlab are you using? Can you maybe upload your `csv` file somewhere so that I can test it? – 0xMB Sep 04 '15 at 16:45
  • I have ML 7.9.0 R2009b. Where could i post the file? – CircAnalyzer Sep 04 '15 at 16:50
  • Each line in your `csv` should contain the same number of values or fields. This is not the case and that's why you get an error. – 0xMB Sep 04 '15 at 17:03
  • so how can i capture the data portion which starts on the second line? – CircAnalyzer Sep 04 '15 at 17:11
  • 1
    I suggest you fix your `csv` file. However what you could do is read the first line using `csvread`. Then read the rest of the file (I'm assuming they all have the same number of fields) using `textscan` or even `csvread`. Then afterwards you can concat these results. – 0xMB Sep 04 '15 at 17:25