필터 지우기
필터 지우기

want to skip the text lines in a file

조회 수: 2 (최근 30일)
Moon Datta
Moon Datta 2012년 11월 2일
댓글: SYED AZHAR 2018년 2월 14일
I have a text file in which first few lines are text. after than the numeric data is there. I want to skip the text lines.The number of text line is also not fixed. But data always start after the string "BEGIN DATA". I have tried but I am not getting any way to read the numeric data ignoring the text lines. my data file looks like
"gdeudfghdjcklkscdjdjljmdxsjdjmjmdjdckdckshfihsklkdkshncfsdjgbfsdgfvdefhvdfjcs
klhnfcjksdzhbsfvbhdslhfc.siafslfkdl;s
hfcsddjkhfcsjldifgdsfgdrjlsjfkldjksdfgvjdsgvbjdshngffgksl
dsghbfvdjkshghrjgujskdhna,h
BEGIN DATA
1
5
2
3
6
74
5
8
6
.
.
I need only
1
5
2
3
6
74
5
8
6
.
.
in a double
Thanks in advance

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 11월 2일
편집: Andrei Bobrov 2012년 11월 2일
f1 = fopen('test2.txt'); % your data in file test2.txt
c = textscan(f1,'%s','Delimiter','\n');
fclose(f1);
out = str2double(c{1}(find(strcmp(c{1},'BEGIN DATA'))+1:end));
out = out(~isnan(out));
or
out = regexp(c{1},'^\d*$','match');
out = str2double(cat(1,out{~cellfun(@isempty,out)}));
or
out = str2double(c{1}(cellfun(@(y)all(~isletter(y))&~isempty(y),c{1})));
  댓글 수: 4
Moon Datta
Moon Datta 2012년 11월 2일
편집: Moon Datta 2012년 11월 2일
I am not understanding what is corrected. actually I need in this way
It will ask me to give the input
I shall give the input
then the program will run
it will give me result
SYED AZHAR
SYED AZHAR 2018년 2월 14일
Go for Gui Interfacing

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2012년 11월 2일
편집: Jan 2012년 11월 2일
Alternatively:
DataStr = fileread('test2.txt');
Key = 'BEGIN DATA';
Index = strfind(DataStr, Key);
Value = sscanf(DataStr(Index + length(Key) + 1), '%g ');

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by