Separating text files according to latitude and longitude
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
dpb
2023-4-20
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 个评论
dpb
2023-5-3
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!