I have a .txt file with over 100,000 rows, want to extract rows that have two specific numbers (latitude and longitude) and save it in Ms Excel.
11 ビュー (過去 30 日間)
古いコメントを表示
I have a very large .txt file with over a 100,000 rows.
I want to extract rows (from the .txt file) that have a specific latitude and longitude and save the extracted rows in Excel.
I currenlty have this code but when I run it I get an error : unexpected MATLAB expression (in the first line of code)
function PET('text_file', num1, num2);
% Open the text file in read mode
fid = fopen(data_2018.txt, 'r');
% Create a new Excel file
excel_file = [data_2018.txt '.xlsx'];
xlswrite(excel_file, []);
% Iterate through the lines in the text file
while ~feof(fid)
% Read the current line
line = fgetl(fid);
% Split the line into a cell array
line_array = strsplit(line, ' ');
% Check if the line contains the two specified numbers
if ismember(27.249, line_array) && ismember(-82.380, line_array)
% Copy the line to the Excel file
xlswrite(excel_file, line);
end
end
% Close the text file
fclose(fid);
end
2 件のコメント
Dyuman Joshi
2023 年 7 月 20 日
編集済み: Dyuman Joshi
2023 年 7 月 20 日
In the definition of a function, the input arguements to the function are supposed to be variables (placeholders) and not values (char or otherwise).
% vvvvvvvvvvv
function PET('text_file', num1, num2);
Apart from this your code has many errors (for the lack of better word) -
1 - It's not clear to me why you have defined input arguements, when your code doesn't use them.
2 - You are supposed to use string or char vector as input to fopen
3 - I don't understand what this line is supposed to do. Possible concatenation to make a string, but then the implementation is questionable.
excel_file = [data_2018.txt '.xlsx'];
4 - If the variable line_array is a cell array, how are you going to compare a numeric value to it?
% Split the line into a cell array
line_array = strsplit(line, ' ');
% Check if the line contains the two specified numbers
if ismember(27.249, line_array) && ismember(-82.380, line_array)
採用された回答
Pratyush
2023 年 7 月 25 日
編集済み: Pratyush
2023 年 7 月 25 日
Hi Leslie,
I understand that you have a text file with multiple lines in which some lines contains a particular pair of number(latitude, longitude) written in between. You want to extract those lines in a row of some Excel sheet. This could be done using the following lines in MATLAB.
% form an array of lines from your text file
fileContent = fileread('your_file.txt');
lines = splitlines(fileContent);
latitude = 42;
longitude = 73;
matchingLines = {};
% iterate through lines and add the desired line to matching line
for i = 1:numel(lines)
line = lines{i};
% ensure the order of latitude and longitude
% because (lat,lon) isn't same as (lon, lat)
pattern = sprintf('%d.*%d', latitude, longitude);
if ~isempty(regexp(line, pattern, 'once'))
matchingLines{end+1, 1} = line;
end
end
% write matchingLines to excel sheet
xlswrite('extracted_data.xlsx', matchingLines, 'Sheet1', 'A1');
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!