reading n columns and n rows from a file

8 views (last 30 days)
Shivik Garg
Shivik Garg on 2 Jul 2018
Answered: Paolo on 2 Jul 2018
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 n lines and n columns from a file starting from line that reads 0.135 My output should look like this
-69.08,-68.8684,-65.2029,-66.4833
-69.0735,-68.8621,-65.2016,-66.48
-69.0605,-68.8496,-65.1989,-66.4733
-69.054,-68.8433,-65.1976,-66.47
  1 Comment
Paolo
Paolo on 2 Jul 2018
Can you please remove the first block of code and add the text file instead?

Sign in to comment.

Answers (1)

Paolo
Paolo on 2 Jul 2018
Save your .txt file as file.dat. Then use:
mydata = csvread('file.dat');
[row,col]=find(ismembertol(mydata,0.1350));
n = 4;
mymatrix = mydata(row+1:row+n,col+1:col+n);

Community Treasure Hunt

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

Start Hunting!