MATLAB Answers

Importing specific blocks of rows from file

10 views (last 30 days)
Gabriel Cortes
Gabriel Cortes on 3 Jul 2017
Commented: Gabriel Cortes on 4 Jul 2017
Hi - I am trying to import data that is distributed like this:
$ [marker]
[data, 12 columns, ~200k rows] ==> this is the data i want to import after the marker
[variable number of columns and rows]
$ [marker] -- Begins another set or Block
[data, 12 columns, ~200k rows] ==> this is the data i want to import after the marker
[variable number of columns and rows]
================================================ This is a sample of the Data
$ 33, 1.000000e+02 ================>> [marker, ID, Timestep]
2.16774183865960e+03, 2.17155003512612e+03, 2.16743573002462e+03, 2.16360580614394e+03, 2.16346774309583e+03, 2.16053340274031e+03, 2.16167864768523e+03, 2.15753149798806e+03, 2.15897246203298e+03, 2.15326408153715e+03, 2.15120660138186e+03, 2.14757146384007e+03,
2.14513985262235e+03, 2.13873969626383e+03, 2.14152811665221e+03, 2.13484192927493e+03, 2.13228154897046e+03, 2.12820999931608e+03, 2.12566138709717e+03, 2.12126497354156e+03, 2.12263910079983e+03, 2.11778548748744e+03, 2.11746577100915e+03, 2.11491733662709e+03,
I tried using 'textscan' but it only imports the first block of ~200k rows, how can I have it move to the next block ?
Dac = fopen('Rat10m_sat_asc3h2.txt','r');
hDat = textscan(Dac,'%f %f %f %f %f %f %f %f %f %f %f %f','Delimiter',',');
thanks for comments, G

  0 Comments

Sign in to comment.

Answers (1)

Anushi1998
Anushi1998 on 4 Jul 2017
You can import the whole file and make an another variable by selecting only specific rows
file=importdata('file_name.txt');
rows=[...] %desired rows
cnt=10
for ii=1:length(rows)
cnt=cnt+1;
data(cnt,:)=file(rows(ii),:);
end
For more help and tutorials refer MATLAB Helper

  1 Comment

Gabriel Cortes
Gabriel Cortes on 4 Jul 2017
Thank you Anushi for response. For this code I would have to identify the beginning and end of each block, which makes it cumbersome. I am looking for a more automatic solution, one that identifies the marker [$] and import fix number of rows below it, skipping the following [not-fixed number of] rows until the next marker comes along in the file. regards,

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!