how to extract a specific text/string from text file

35 次查看(过去 30 天)
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
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
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'));

类别

Help CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by