MATLAB Answers

0

How to read only numerical data from irregular .csv file

Asked by Juan Makaka on 12 Mar 2019
Latest activity Commented on by Juan Makaka on 13 Mar 2019
I have a .csv file with header and numerical data. I just want to get the numerical data starting in row 6 and col 2. I am trying to use textscan but I failed to get the data. This is my code and one of my files. Any idea for that ?. Thanks.
fid = fopen('00_IR_00327 - T0R1P2.csv','rt');
NumHeaders = 5;
NumDataLines = 240;
ColNum = 320;
fmt =['%*s', repmat('%f',1,ColNum-1),'\r\n'];
data = textscan(fid, fmt, NumDataLines, 'HeaderLines', NumHeaders,'Delimiter',',');
fclose(fid);

  0 Comments

Sign in to comment.

1 Answer

Answer by Akira Agata
on 13 Mar 2019

Since your csv file was saved as a 16-bit text, it's a little bit difficult to use textscan to read data correctly.
I think another possible way is using xlsread function. How about the following?
[~,~,c] = xlsread('00_IR_00327 - T0R1P2.csv');
data = [];
for kk = 6:numel(c)
str = strsplit(c{kk},',');
data = [data;str2double(str)];
end
% Delete 1st column and the last empty column
data(:,[1 end]) = [];

  1 Comment

It worked great, thanks Akira!.

Sign in to comment.