필터 지우기
필터 지우기

Extracting and formatting data from a txt like file

조회 수: 1 (최근 30일)
Bernard
Bernard 2013년 8월 19일
Hello, I have a text file that's actually a .hoc file but I can't figure out how to convert it to a different format so I'm trying to extract data from it. I imported it into Matlab and what i have is cell array. the beginning of each cell has some weird text string that is useless to me, i just want the data after it. e.g.
pt3dadd(90.8322,34.7489,83.0711,1.25825,0)
pt3dadd(90.6025,34.8644,83.3744,1.25825,0)
I need to take those points (actually only the first numbers and put them into a new array. should I try to delete the 'pt3dadd' or is there a way I can read it and then ignore that?

채택된 답변

Cedric
Cedric 2013년 8월 19일
편집: Cedric 2013년 8월 19일
You can do something like:
fid = fopen('myFile.hoc', 'r') ;
content = textscan(fid, 'pt3dadd(%f,%f,%f,%f,%f)') ;
fclose(fid) ;
and you will have numbers in cell array content.
  댓글 수: 2
Bernard
Bernard 2013년 8월 19일
I forgot to mention the file has a lot more random text and such so there are headers and other numerical data
Cedric
Cedric 2013년 8월 19일
편집: Cedric 2013년 8월 19일
Ok, so the whole file is not completely structured. Then I'd say that regular expressions would be a good option, e.g.:
% - Read file in one shot.
buffer = fileread('myFile.hoc') ;
% - Extract fields as strings.
pattern = 'pt3dadd\(([\d\.]+),([\d\.]+),([\d\.]+),([\d\.]+),([\d\.]+)' ;
content = regexp(buffer, pattern, 'tokens') ;
% - Restructure cell array and convert to double.
content = reshape([content{:}], 5, []).' ;
content = str2double(content) ;
By the way, did you need more information in this previous thread of yours? : http://www.mathworks.com/matlabcentral/answers/84367-ridiculously-simple-nearest-neighbor-search-3d

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by