how to extract a specific text/string from text file

22 ビュー (過去 30 日間)
Iñigo Molina Sánchez
Iñigo Molina Sánchez 2020 年 11 月 21 日
コメント済み: Image Analyst 2020 年 11 月 22 日
I have the following text file:
.................................................................
POS, 619857792, 41.6477224, -4.2899403, 874.34, -12.55482
IMU, 619875777, -0.06479891, 0.2502703, 0.0502184, -0.4383386, -0.129217, -14.2286, 0, 0, 32.03047, 1, 1
IMU2, 619875777, -0.08689992, 0.2353809, 0.04117718, -0.756902, 0.09682357, -14.2211, 0, 0, 34.875, 1, 1
CAM, 619876179, 469709600, 2132, 41.6477203, -4.289937, 874.33, -11.96, 873.32, 2.73, 0.55, 106.3
ARSP, 619876621, 15.70767, 136.6173, 11.25, 136.6173, 1.993906, 1
IMU, 621675604, -0.005321865, 0.09623767, 0.01352201, -0.5002327, 0.789924, -10.80867, 0, 0, 31.89751, 1, 1
IMU2, 621675604, -0.03922234, 0.1098862, 0.009741771, -0.5334755, 0.7404263, -10.89349, 0, 0, 34.625, 1, 1
CAM, 621676033, 469711400, 2132, 41.6475301, -4.2896673, 875.32, -10.97, 873.98, -2.65, 4.01, 111.77
MAG, 621676323, -107, -268, 373, 93, -78, -30, 0, 0, 0, 1, 621676239
MAG2, 621676323, -137, -168, 253, 145, 211, -550, 0, 0, 0, 1, 621676242
ARSP, 621676575, 14.68222, 119.3618, 11.25, 119.3618, 1.993906, 1
............................................................................................................................
I want to extract the lines with the item CAM, so the result is:
CAM, 619876179, 469709600, 2132, 41.6477203, -4.289937, 874.33, -11.96, 873.32, 2.73, 0.55, 106.3
CAM, 621676033, 469711400, 2132, 41.6475301, -4.2896673, 875.32, -10.97, 873.98, -2.65, 4.01, 111.77
Can someone help me with this the code? Thank you very much
  2 件のコメント
Rik
Rik 2020 年 11 月 21 日
What did you try? Do you already know how to read your file to a Matlab variable? Are you running R2020b?
Iñigo Molina Sánchez
Iñigo Molina Sánchez 2020 年 11 月 21 日
Rik, Thank you very much for your answer. Yes, I'm running R2020b. I tried something like this:
fid = fopen('2020-11-20 11-23-40.log','r');
while ~feof(fid)
st = fgetl(fid);
if ~isempty(findstr(st,'CAM,'))
ftell(fid)
fseek(fid,-9,0)
F = cell2mat(textscan(fid,'%5d'))
end
end
However, i know I'm doing something wrong

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2020 年 11 月 21 日
Try this:
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
% See if this line starts with CAM
if startsWith(textLine, 'CAM')
% Do something with this line.
end
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the file.
fclose(fileID);
  5 件のコメント
Iñigo Molina Sánchez
Iñigo Molina Sánchez 2020 年 11 月 22 日
Hi Rik, I agree completely with you. Indeed, I already add the corresponding sentence.
Thank you very much again!
Image Analyst
Image Analyst 2020 年 11 月 22 日
I'd probably open the output file as text explicitly with the 'wt' option since a CSV file is just a plain, flat text file:
fiD2 = fopen('CAMfile.csv', 'wt');

サインインしてコメントする。

その他の回答 (1 件)

Rik
Rik 2020 年 11 月 21 日
You can either use my readfile function (which you can get from the file exchange or the add-on manager). Another option is to use the readlines function.
Once you have read the file with either of these functions, you can use contains to create a logical array to index it:
Data=readfile(filename);
Data=Data(contains(Data,'CAM'));
  1 件のコメント
Iñigo Molina Sánchez
Iñigo Molina Sánchez 2020 年 11 月 21 日
Thank you very much Rik. I wil try your suggestions.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by