Find specific vector in different text files
조회 수: 1 (최근 30일)
이전 댓글 표시
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/160182/image.jpeg)
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
채택된 답변
Guillaume
2017년 1월 27일
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].
추가 답변 (1개)
KSSV
2017년 1월 27일
fid = fopen('your text file') ;
distance = textscan(fid,'%f','Headerlines',15,'delimiter','\n') ;
fclose(fid) ;
댓글 수: 6
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!