필터 지우기
필터 지우기

How to read all lines in txt file satisfying some condition

조회 수: 4 (최근 30일)
Mateusz
Mateusz 2013년 10월 22일
편집: Cedric 2013년 10월 22일
Let's assume we have the following format in the file: 1 str1 0 str2 1 str3 ...
that is in each line we have: the first character is number, either 0 or 1, next a whitespace (space or tab) and next a string.
I would like to get all strings that have 1 as first characted in the line. How to do this efficiently (and elegantly, preferably with one-liner). I know I can use fgets and just iterate over the lines.

답변 (1개)

Cedric
Cedric 2013년 10월 22일
편집: Cedric 2013년 10월 22일
The following is assuming that you have one pair number/string per line in the file.
match = regexp( fileread('myFile.txt'), '(?<=1 )[^\r\n]+', 'match' ) ;
but one-liner doesn't mean efficient or elegant. You would have to compare this regexp-based approach to approaches based on FGETL, FSCANF, or TEXTSCAN ..

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by