Find specific vector in different text files
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
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
el 27 de En. de 2017
Christian
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) ;
Christian
el 27 de En. de 2017
KSSV
el 27 de En. de 2017
It did work for the dummy text files you have attached....
Categorías
Más información sobre Text Data Preparation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!