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 from csv file

Subject: reading from csv file

From: gouthami

Date: 7 Dec, 2012 08:11:19

Message: 1 of 2

hi i have csv file of format

name, value0, value1, value2, time
K3G Gyroscope Sensor, 0.254425, 0.081245, -0.479835, 921218
K3DH Acceleration Sensor, 1.184970, 2.901134, 9.275456, 921377
K3G Gyroscope Sensor, 0.179289, -0.474642, -0.402560, 921405
K3DH Acceleration Sensor, 1.389275, 2.574246, 9.016670, 921577
K3G Gyroscope Sensor, 0.089186, -2.804177, -1.305419, 921592
K3DH Acceleration Sensor, 4.249548, 2.887514, 8.199450, 921777
K3G Gyroscope Sensor, 0.946230, -2.365881, -0.635300, 921786
K3G Gyroscope Sensor, -0.495717, -1.606881, 0.089492, 921980
K3DH Acceleration Sensor, 6.101916, 4.195067, 6.088295, 921986

i just want to read value 0 , 1,2 time rows of colums having only 'K3DH Acceleration Sensor'

i.e i want to read only value 0,1,2 time of following lines...

name, value0, value1, value2, time
K3DH Acceleration Sensor, 1.184970, 2.901134, 9.275456, 921377
K3DH Acceleration Sensor, 1.389275, 2.574246, 9.016670, 921577


i used below code
% Split header
A(1,:) = regexp(tline, '\,', 'split');

% Parse and read rest of file
ctr = 1;
while(~feof(fid))
if ischar(tline)
ctr = ctr + 1;
tline = fgetl(fid);
A(ctr,:) = regexp(tline, '\,', 'split');
else
break;
end
end
fclose(fid);

sensor=A(:,1);
x1=A(:,2);
y1=A(:,3);
z1=A(:,4);
t1=A(:,5);

from this i am just able to seperate rows in csv...how to read particular line i want to read..

thanks in advance

Subject: reading from csv file

From: dpb

Date: 7 Dec, 2012 19:53:09

Message: 2 of 2

On 12/7/2012 2:11 AM, gouthami wrote:
> hi i have csv file of format
>
> name, value0, value1, value2, time
> K3G Gyroscope Sensor, 0.254425, 0.081245, -0.479835, 921218
> K3DH Acceleration Sensor, 1.184970, 2.901134, 9.275456, 921377
> K3G Gyroscope Sensor, 0.179289, -0.474642, -0.402560, 921405
...
>
> i just want to read value 0 , 1,2 time rows of colums having only 'K3DH
> Acceleration Sensor'
>
> i.e i want to read only value 0,1,2 time of following lines...
>
> name, value0, value1, value2, time
> K3DH Acceleration Sensor, 1.184970, 2.901134, 9.275456, 921377
> K3DH Acceleration Sensor, 1.389275, 2.574246, 9.016670, 921577
>
>
> i used below code
> % Split header
> A(1,:) = regexp(tline, '\,', 'split');
>
> % Parse and read rest of file
> ctr = 1;
> while(~feof(fid))
> if ischar(tline) ctr = ctr + 1;
> tline = fgetl(fid); A(ctr,:) = regexp(tline, '\,', 'split'); else
> break; end
> end
> fclose(fid);
>
...

I'd go at i a lot more straightahead...read the data and then save what
you want...

 >> [s,x,y,z,t]=textread('yourfilename', ...
               '%s %f %f %f %d','delimiter',',');
 >> ix=findstr(s,'K3DH Acceleration Sensor')
ix =
      2
      4
      6
      9
 >> dat=[t(ix) x(ix) y(ix) z(ix)]
dat =
   1.0e+005 *
     9.2138 0.0000 0.0000 0.0001
     9.2158 0.0000 0.0000 0.0001
     9.2178 0.0000 0.0000 0.0001
     9.2199 0.0001 0.0000 0.0001
 >> clear t x y z

Or, of course, you can change the logic of the test and delete the lines
not wanted--your choice.

Vice versa, you could read a line at a time and then sscanf() the
desired lines but unless file is really, really large it's easier to
just process in memory...

--

Tags for this Thread

No tags are associated with 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