MATLAB: Read a text file and write the output in a text file after excluding some rows have values in particular range

dataif statementmaketextfiletext filetextscan

I have a text file, which contains four columns (Latitude, Longitude ,Depth and magnitude but I need to exclude some rows that fall in a particular range of Latitude and longitude.And write the output in a new text file in four distinct column as Latitude Longitude Depth and magnitude respectively.
Range of values to exclude
  • Latitude : 25 to 30 and Longitude 85 to 90(There is more of this criteria though)
Hope someone can help me with this!

Best Answer

The neat but slightly slower way of doing it:
t = readtable('inputmat.txt');
t.Properties.Variablenames = {'Latitude', 'Longitude', 'Depth', 'Magnitude'};
t((t.Latitude >= 25 & t.Latitude <= 30) | (t.Longitude >= 70 & t.Longitude <= 72), :) = []; writetable(t, 'newfile.txt', 'WriteVariableNames', false, 'Delimiter', 'tab');
It's very easy to understand what the code does, even without comments, thanks to the usage of column names.
The slightly faster, but more obscure way:
m = dlmread('inputmat.txt');
m((m(:, 1) >= 25 & m(:, 1) <= 30) | (m(:, 2) >= 70 & m(:, 2) <= 72), :) = [];
dlmwrite('newfile.txt', m, '\t');
I personally, would favor the first version.