How to read a .txt file and cnvert the data to a table
조회 수: 9 (최근 30일)
이전 댓글 표시
I have a .txt file with time, latitude and longitude data. I want to convert the data into a table with columns time, latitide and longitude for processing further. I have attached the file. I also want to remove the first two lines and the last line. How can I do that?
댓글 수: 1
Askic V
2023년 4월 28일
It seems to me that you first need to check the structure of your data. Some rows seems to be not placed propely.
atit1 : Time : 05:18:16.00
Latitude : 28.544998 : Longitude : 77.189e : 05:18:16.00
Latitude : 28.544998 : Longitude : 77.189971 : Time : 05:18:16.00
Latitude : 28.544998 : Longitude : 77.1899 : 05:18:16.00
Latitude : 28.544998 : Longitude : 77.189971 : Time : 05:18:16.00
Latitude : 28.544998 : Longitude : 77.1899Latitude : 28.545000 : Longitude : 77.189971 : Time : 05:18:18.00
Latitude : 28.545000 : Longitude : 77.189971 : Time : 05:18:18.00
채택된 답변
Suraj
2023년 4월 28일
Hi Kanica
I see that you'd like to derive a table from the text file provided. You can do so with the following code:
% read the table from the file 'gps_values.txt', use space as the delimiter,
% HeaderLines is set to 2 to ingore the first two lines
raw_data = readtable('gps_values.txt', 'Delimiter', ' ', 'HeaderLines', 2, 'ReadVariableNames', false);
% create a new table with only columns 3, 8, and 13 from the original table
% These are the columns that contain the latitude, longitude and time
% respectively.
data = raw_data(:, [3 8 13]);
% assign variable names to the new table columns
data.Properties.VariableNames = {'Latitude' 'Longitude' 'Time'};
% display the new table
data
The above could should the read the data in your "gps_values.txt" file into a table. However it is worth noting that the format should be consistent at every line otherwise you may encounter errors.
Hope this helps.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!