How to read only some lines and skip others

2 views (last 30 days)
time,n0v_soma,n1v_soma,n2v_soma,n4v_dend,n5v_dend,n6v_dend,n7v_dend,n8v_dend,n9v_dend,n10v_dend,n11v_dend,n12v_dend,n13v_dend,n14v_dend,n15v_dend,n16v_dend,n17v_dend,n18v_dend,n19v_dend,n20v_dend,n21v_dend,n22v_dend,n23v_dend,n0Isyn,n1Isyn,n2Isyn,n0curr,n1curr,n2curr
0,-69.1127,-68.9,-65.2096,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-66.5,-2,-2,-2,0,0,0
0.027,-69.1062,-68.8937,-65.2083,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-66.4966,-2,-2,-2,0,0,0
0.054,-69.0996,-68.8874,-65.2069,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-66.4933,-2,-2,-2,0,0,0
0.081,-69.0931,-68.8811,-65.2056,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-66.4899,-2,-2,-2,0,0,0
0.108,-69.0865,-68.8747,-65.2043,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-66.4866,-2,-2,-2,0,0,0
0.135,-69.08,-68.8684,-65.2029,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-66.4833,-2,-2,-2,0,0,0
0.162,-69.0735,-68.8621,-65.2016,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-66.48,-2,-2,-2,0,0,0
0.189,-69.067,-68.8559,-65.2003,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-66.4766,-2,-2,-2,0,0,0
0.216,-69.0605,-68.8496,-65.1989,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-66.4733,-2,-2,-2,0,0,0
0.243,-69.054,-68.8433,-65.1976,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-66.47,-2,-2,-2,0,0,0
I want to read only the first 4 lines and first 4 columns skip the rest and also want to skip the header the output should be like this.
0,-69.1127,-68.9,-65.2096,
0.027,-69.1062,-68.8937,-65.2083
0.054,-69.0996,-68.8874,-65.2069
0.081,-69.0931,-68.8811,-65.2056

Accepted Answer

per isakson
per isakson on 7 Feb 2017
Edited: per isakson on 7 Feb 2017
>> cssm('cssm.txt')
ans =
0 -69.1127 -68.9000 -65.2096
0.0270 -69.1062 -68.8937 -65.2083
0.0540 -69.0996 -68.8874 -65.2069
0.0810 -69.0931 -68.8811 -65.2056
where
function out = cssm( filespec )
fid = fopen( filespec );
cac = textscan( fid, '%f%f%f%f%*[^\n]', 4, 'Delimiter',',' ...
, 'CollectOutput',true, 'Headerlines',1 );
[~] = fclose( fid );
out = cac{:};
end
and cssm.txt contains your data
  5 Comments
Walter Roberson
Walter Roberson on 7 Feb 2017
want_rows = 4;
want_cols = 100;
fmt = [repmat('%f', 1, want_cols), '%*[^\n]'];
cac = textscan( fid, fmt, want_rows, 'Delimiter',',' ...
, 'CollectOutput',true, 'Headerlines',1 );
per isakson
per isakson on 7 Feb 2017
And combine all of it to
function out = cssm( filespec, want_rows, want_cols )
....
end

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!