how to extract a specific text/string from text file
显示 更早的评论
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
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
2020-11-21
采纳的回答
更多回答(1 个)
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'));
类别
在 帮助中心 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!