How to delete file if if have contain wrong value

1 view (last 30 days)
Hi,
I have 700 ".txt" file that contain longitude and latitude. I want to keep data which have longitude 136<=long<=146, latitude 34<=lat<=41 and delete the other.
Does anyone have an idea how to do this? I have wrote the code, but it still return error.
--------------------------------------------------------
inputfolder = 'K:\Geonet data_2011\indata';
outputfolder = 'K:\Geonet data_2011\outdata';
numfiles = 1243;
%mydata = cell (1, numfiles);
for k = 1:numfiles
inputfile = sprintf ('point%d.txt', inputfolder, k);
data {k} = importdata (inputfile);
x = load (inputfile);
lat = x (:,8);
long = x (:,9);
if (lat<=41 && lat>=34)
if (long<=146 && long>=136)
inputfile = [];
end
end
outputfile = sprintf ('%6d.11.pos' , outputfolder, k);
save (outputfile, '%6d.11.pos')
  2 Comments
Image Analyst
Image Analyst on 13 Jun 2014
Please clarify. Do you want to delete a file (like it says in your subject line), or keep/save a file (like it says in the body of your message), or both?
hidayat
hidayat on 16 Jun 2014
Edited: hidayat on 16 Jun 2014
I want to delete a file which have a longitude and latitude value greater or lower than constrain value (as I write below) and keep the file if the file has value within the constrain value.
if (lat<=41 && lat>=34)
if (long<=146 && long>=136)
Do you have any suggestion for me? thank you in advance.

Sign in to comment.

Answers (1)

Voss
Voss on 20 Dec 2023
inputfolder = 'K:\Geonet data_2011\indata';
outputfolder = 'K:\Geonet data_2011\outdata';
numfiles = 1243;
for k = 1:numfiles
inputfile = fullfile(inputfolder,sprintf('point%d.txt',k));
% x = readmatrix(inputfile);
x = load(inputfile);
lat = x(:,8);
long = x(:,9);
to_keep = long >= 136 & long <= 146 & lat >= 34 & lat <= 41;
x = x(to_keep,:);
outputfile = fullfile(outputfolder,sprintf('%6d.11.pos',k));
% writematrix(x,outputfile);
save(outputfile,'x','-ascii');
end
readmatrix and writematrix (since R2019a) are preferred over load and save.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!