Effacer les filtres
Effacer les filtres

want to skip the text lines in a file

2 vues (au cours des 30 derniers jours)
Moon Datta
Moon Datta le 2 Nov 2012
Commenté : SYED AZHAR le 14 Fév 2018
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

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 2 Nov 2012
Modifié(e) : Andrei Bobrov le 2 Nov 2012
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 commentaires
Moon Datta
Moon Datta le 2 Nov 2012
Modifié(e) : Moon Datta le 2 Nov 2012
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 le 14 Fév 2018
Go for Gui Interfacing

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 2 Nov 2012
Modifié(e) : Jan le 2 Nov 2012
Alternatively:
DataStr = fileread('test2.txt');
Key = 'BEGIN DATA';
Index = strfind(DataStr, Key);
Value = sscanf(DataStr(Index + length(Key) + 1), '%g ');

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by