Find specific vector in different text files

Hello everybody!
I have several different text-files, which look very similar but have minor differences. I have created a dummy text file, to show you how my files look:
Note: Everything in this dummy is "fake".
Now I have a huge number of files like this. But the only thing I need is the distance vector which is named "Distance" in every file.
I tried this code, but it doesn't work so far:
[pathstr,filename{actualfilenumber},ext] = fileparts(files{actualfilenumber});
complete_file_name{actualfilenumber} = fullfile(Path,char([filename{actualfilenumber},ext]));
actualfile = textscan(complete_file_name{actualfilenumber},'%s');
Distance = double(actualfile(:,find(ismember('Distance',actualfile))));
Any suggestions?
Cheers Christian

2 comentarios

KSSV
KSSV el 27 de En. de 2017
Attach your text file here...
Christian
Christian el 27 de En. de 2017
Here are two dummy files:

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 27 de En. de 2017
filecontent = fileread('DummyText1.txt');
distances = str2double(strsplit(regexp(filecontent, '(?<=Distance.*[\n\r]+)[ 0-9.\n\r]*(?=/Distance)', 'match', 'once', dotexceptnewline')));
distances(isnan(distances)) = [];
As long as the distance numbers are not signed and not in scientific notations. To support signed numbers replace [ 0-9.\n\r] by [-+ 0-9.\n\r]. To support scientific notation in addition, replace it with [-+ 0-9.e\n\r].

Más respuestas (1)

KSSV
KSSV el 27 de En. de 2017
fid = fopen('your text file') ;
distance = textscan(fid,'%f','Headerlines',15,'delimiter','\n') ;
fclose(fid) ;

6 comentarios

Christian
Christian el 27 de En. de 2017
The problem is, the distance-vector does NOT start at line 15 all the time. Sometimes there are 30 lines of additional information above sometimes none.
KSSV
KSSV el 27 de En. de 2017
Editada: KSSV el 27 de En. de 2017
Okay..it can be sorted...Attach one text file....
Christian
Christian el 27 de En. de 2017
I have just attached two dummy files to the question!
KSSV
KSSV el 27 de En. de 2017
Editada: KSSV el 27 de En. de 2017
fid = fopen('Dummy_Text1.txt') ;
S = textscan(fid,'%s','delimiter','\n') ;
fclose(fid) ;
S = S{1} ;
% find position of distance
idx = strfind(S, 'Distance');
idx = find(not(cellfun('isempty', idx)));
% get distance array
distance = cell2mat(S(idx(1)+1:idx(2)-1)) ;
distance = str2num(distance) ;
Well, that does not work:
Attempted to access idx(1); index out of bounds because numel(idx)=0.
Error in Dummy (line 14)
distance = cell2mat(S(idx(1)+1:idx(2)-1));
KSSV
KSSV el 27 de En. de 2017
It did work for the dummy text files you have attached....

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 27 de En. de 2017

Comentada:

el 27 de En. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by