HOW TO EXTRACT VALUES FROM MULTIPLE TEXT FILE

11 views (last 30 days)
I have multiple text file of latitude and longitude(data_27.75_92.45, data_27.75_92.55......). Each text file contain a single value.
I want to extract the single value of each text file in excel format. I have 15609 files. Can anybody help me?
  1 Comment
dpb
dpb on 2 Apr 2023
Are these really text files and how/why in the world did you create 15,000 files instead of putting the values into a single file to begin with????
If they are text files, then just use the OS and copy/concatenate them all; then read the resultant file.
Attach a couple the files to be sure we know what it is you're actually dealing with if we have to, but...first let's see if we can avoid creating the problem in the first place. Explain how you got these files to start with...

Sign in to comment.

Accepted Answer

Neha
Neha on 5 Apr 2023
It is my understanding that you have several text files where each file has its content in the following format: data_latitudeValue_longitudeValue and you want to create an excel file with data from all the text files. Please refer to the code given below, it creates an excel file with two columns: latitude and longitude along with their values.
directory = '/path/to/your/folder';
lat_lon_values = zeros(15609, 2);
file_names = dir(fullfile(directory,'*.txt'));
for i = 1:numel(file_names)
file_path = fullfile(directory,file_names(i).name);
file_content = fileread(file_path);
values=strsplit(file_content,"_");
lat_lon_values(1,i) = str2double(values{2});
lat_lon_values(2,i)=str2double(values{3});
end
T = table(lat_lon_values(1),lat_lon_values(2), 'VariableNames', {'Latitude','Longitude'});
writetable(T, 'lat_lon_values.xlsx');

More Answers (0)

Categories

Find more on Data Import and Export 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!