how to extract a specific text/string from text file
38 views (last 30 days)
Show older comments
Iñigo Molina Sánchez
on 21 Nov 2020
Commented: Image Analyst
on 22 Nov 2020
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 Comments
Rik
on 21 Nov 2020
What did you try? Do you already know how to read your file to a Matlab variable? Are you running R2020b?
Accepted Answer
Image Analyst
on 21 Nov 2020
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 Comments
Image Analyst
on 22 Nov 2020
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');
More Answers (1)
Rik
on 21 Nov 2020
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'));
See Also
Categories
Find more on Standard File Formats 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!