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

Thread Subject:
Reading CSV file

Subject: Reading CSV file

From: Rakesh

Date: 12 Feb, 2011 16:20:06

Message: 1 of 7

My CSV file looks as below:
date,time, M01, M02, M03, M04, M05, M06
 2/11/2011, 0:10, 7.1, 8.1, 8.1, 7.7, 0, 8.1
 2/11/2011, 0:30, 5.6, 6.8, 7.1, 6.6, 0, 6.8
 2/11/2011, 0:50, 4.6, 5.5, 6.1, 5.8, 0, 5.6
Can you help me to read this CSV file properly so I can convert the first two column into a string using datenum?
What I did is:
fid=fopen(file1);
data_f1=textscan(fid,'%s %s %f %f %f %f %f %f', 'delimiter',',','collectoutput',1,'headerlines',1);
fclose(fid);

now data_f1{1} has date & time in separate coloums. I want to use datenum to convert date & time. Is there any way to do this without using 'for loop' for each set of date & time. If both date & time can come as a single string i.e. in single coloum of cell array then it can be done simply by using datenum(data_f1{1}). But now I may have to use iterations to combine both date and time and then convert it, for each set. Please suggest if it can be done without using iterations.

Subject: Reading CSV file

From: Franck Dernoncourt

Date: 12 Feb, 2011 17:46:04

Message: 2 of 7

Check out strcat.

Example:
strcat(data_f1{1}(:, 1),data_f1{1}(:, 2))

Subject: Reading CSV file

From: Rakesh

Date: 12 Feb, 2011 18:39:03

Message: 3 of 7

 strcat(data_f1{1}(:, 1),data_f1{1}(:, 2))
is working but its not giving space between date & time, i.e.
'2011-02-070:00:00' but I need '2011-02-07 0:00:00'
So datenum won't give desired result. To generate space between date & time I tried:
strcat({data_f1{1}(:, 1)},{' '},{data_f1{1}(:, 2)})
but it is giving error:
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Subject: Reading CSV file

From: TideMan

Date: 12 Feb, 2011 19:08:03

Message: 4 of 7

On Feb 13, 7:39 am, "Rakesh " <errakeshj...@gmail.com> wrote:
>  strcat(data_f1{1}(:, 1),data_f1{1}(:, 2))
> is working but its not giving space between date & time, i.e.
> '2011-02-070:00:00' but I need '2011-02-07 0:00:00'
> So datenum won't give desired result. To generate space between date & time I tried:
> strcat({data_f1{1}(:, 1)},{' '},{data_f1{1}(:, 2)})
> but it is giving error:
> ??? Error using ==> horzcat
> CAT arguments dimensions are not consistent.

So, tell datenum what your format is:
t=datenum(strcat(data_f1{1}(:, 1),data_f1{1}(:, 2)),'dd/mm/
yyyyHH:MM');

Subject: Reading CSV file

From: Rakesh

Date: 12 Feb, 2011 19:28:03

Message: 5 of 7

Thanks Tideman & Franck. Problem resolved.

Subject: Reading CSV file

From: Pekka Kumpulainen

Date: 12 Feb, 2011 19:31:03

Message: 6 of 7

"Rakesh" wrote in message <ij6k47$bjd$1@fred.mathworks.com>...
> strcat(data_f1{1}(:, 1),data_f1{1}(:, 2))
> is working but its not giving space between date & time, i.e.
> '2011-02-070:00:00' but I need '2011-02-07 0:00:00'
> So datenum won't give desired result. To generate space between date & time I tried:
> strcat({data_f1{1}(:, 1)},{' '},{data_f1{1}(:, 2)})
> but it is giving error:
> ??? Error using ==> horzcat
> CAT arguments dimensions are not consistent.

If you add 'Whitespace','' in the textscan the leading spaces are included and you can use strcat and datenum.

Another way is to read all in to single matrix´. Replace %s %s with 5 times %f and add : and / to the delimiter:
data_f1=textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f', 'delimiter',',:/','collectoutput',1,'headerlines',1);
Now you can use datenum with numeric vector (matrix) input, if you first rearrange and add the seconds (assuming the time was in hr:min):
N = size(data_f1{1},1) % number of rows
datenum([data_f1{1}(:,[3 2 1 4 5]) zeros(N,1)])

Subject: Reading CSV file

From: Miroslav Balda

Date: 12 Feb, 2011 21:11:03

Message: 7 of 7

"Rakesh" wrote in message <ij6n03$cj2$1@fred.mathworks.com>...
> Thanks Tideman & Franck. Problem resolved.

Hi Rakesh,
I see, I am too late, nevertheless, the is another solution using function from FEX:
      www.mathworks.com/matlabcentral/fileexchange/18430

A = txt2mat('file1.txt',1,11,'ReplaceChar',{'/, ', ':, '});
n = datenum(A(:,3),A(:,2),A(:,1),A(:,4),A(:,5),...
            zeros(size(A,1),1));
M = A(:,6:11);

fprintf('n = %10.2f\n',n);
fprintf('\n');
fprintf('M = [%4.1f,%4.1f,%4.1f,%4.1f,%4.1f,%4.1f ]\n',M);

with the output:

n = 734809.01
n = 734809.02
n = 734809.03

M = [ 7.1, 5.6, 4.6, 8.1, 6.8, 5.5 ]
M = [ 8.1, 7.1, 6.1, 7.7, 6.6, 5.8 ]
M = [ 0.0, 0.0, 0.0, 8.1, 6.8, 5.6 ]

Good luck,
Mira

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us