Separating text files according to latitude and longitude

I have various text files namely
SNOWBAND_27.775_92.325.txt
SNOWBAND_27.775_92.425.txt
SNOWBAND_27.825_89.225.txt
SNOWBAND_27.825_89.275.txt
SNOWBAND_27.825_92.275.txt
SNOWBAND_27.825_92.325.txt
SNOWBAND_27.825_92.375.txt
SNOWBAND_27.825_92.425.txt
SNOWBAND_27.825_92.475.txt
I have another latlon file which contain latitude and longitude of
27.825 92.275
27.825 92.325
27.825 92.375
I want to separate the text files with matching lat and lon of latlon file.
If there is any way to separate the files.

Answers (1)

Sure. Just read the lat/lon data file, substitute the dot woth underscore and do a dir() with that string in the filename wildcard expression to return the specific file.
latlon=readlines('yourlocationsfile.ext');
for i=1:numel(latlon)
ll=strrep(latlon(i),'.','_');
d=dir("SNOWBAND*"+ll+".txt");
fn=fullfile(d.folder,d.name);
% do whatever with the file here
...
end
First time I ran this I'd probably add the fully-qualified filename to the latlon file to go with it so had them both together; then just read the one file and get what you're looking for. Probably make sense to also separate out the lat/lon data into separate values for lat and long as numeric values to be able to do searches for ranges and/or specific values directly as well. Preprocessing and creating a more useful database could save a bunch of time later on...

9 Comments

Sir,
I run the code upto
clc;
clear all;
latlon=xlsread('C:\Users\DELL\Desktop\New folder (4)\Book1.xlsx');
for i=1:numel(latlon)
ll=strrep(latlon(i),'.','_');
d=dir('SNOWBAND*'+ll+'.txt');
end
It is giving error message Warning: Inputs must be character arrays or cell arrays of strings.
Error using +
Matrix dimensions must agree.
I am attaching my text and excel files.
readlines returns a string array; xlsread is deprecated and will not return string but numeric...need to read as string or convert to string...
Sir it may be silly mistake but when I am evaluating
latlon=readlines('Book1.xlsx'); The error is
Undefined function 'readlines' for input arguments of type 'char'.
I have less knowlege about this.
Well, the spreadsheet file format wasn't provided originally; I presumed it was a similar string representation as the filenames.
If as it seems it's just two columns of lat;lon pairs that are numeric, then
latlon=string(readmatrix('yourlocationsfile.xlsx')); % read matrix, convert string
fnames=compose("SNOWBAND_%s_%s.txt",latlon(:,1),latlon(:,2)); % convert to matching filenames
for i=1:numelfnames
fn=fullfile(yourdatafolder,fnames(i));
% do whatever with the file here
...
end
the above will give an array of filenames matching the patter for the given numeric value -- it relies on the exact match of the numeric value including precision to match; string() will convert the characters it sees as a number to that string; using a numeric format like '%f' will use a variable precision depending upon the magnitude and hardcoding a '%f.3' might not match a file if there were more or fewer digits of precision in some cases.
Sir,
I am using Matlab version less than 2015 and here compose function not found. If there is another way to create fnames.
fnames=compose('SNOWBAND_%s_%s.txt',latlon(:,1),latlon(:,2));
Undefined function 'compose' for input arguments of type 'char'.
I have done latlon to string array by :
latlon=xlsread('C:\Users\hp\Desktop\New folder (4)\Book1.xlsx');
a=strtrim(cellstr(num2str(latlon'))')
An equivalent function to compose is the undocumented sprintfc function —
Y = sprintfc('%d',1:5)
Y = 1×5 cell array
{'1'} {'2'} {'3'} {'4'} {'5'}
.
I have written this code for individual lat lon
clc;
clear all;
latlon=xlsread('C:\Users\DELL\Desktop\New folder (4)\Book1.xlsx');
latlon1=num2str(latlon);
fnames=sprintf('SNOWBAND_%.3f_%.3f.txt',latlon(1,:));
% fnames1=sprintf('SNOWBAND_%.3f_%.3f.txt',latlon(2,:));
fn=fullfile('C:\Users\DELL\Desktop\New folder (4)',fnames);
movefile(fn,'C:\Users\DELL\Desktop\New folder (4)\New folder');
The error is coming :
Error using movefile
No matching files were found.
Use the debugger to step through and see what isn't as you expect.
The conclusion has to be that the file isn't located where you think it is or there isn't one of the particular number that matches the one you're trying to find.
But, we can't see your file structrure from here although I'd strongly recommend against leaving the default names and with spaces in the folder names; it just makes for a mess to have to ensure code against.
What does
dir(fullfile(['C:\Users\DELL\Desktop\New folder (4)' '*.txt']))
return?
Sir,
Thankyou for your suggestion. It works for me. I had to do this for working directory of matlab. Before I didnot use strcat but my input was string array. The code is
clc;
clear all;
latlon=xlsread('C:\Users\hp\Desktop\new\Book1.xlsx');
fnames=sprintf('SNOWBAND_%.3f_%.3f.txt.txt',latlon(1,:));
% fnames1=sprintf('SNOWBAND_%.3f_%.3f.txt',latlon(2,:));
a=strcat(fnames);
fn=fullfile('C:\Users\hp\Documents\MATLAB',a);
movefile(fn,'C:\Users\hp\Documents\MATLAB\New Folder');
My query is I able to extract file for individual latlon. If I have to do for multiple lat lon how can I modify the code? Any suggestion.

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!