Extracting and formatting data from a txt like file

1 次查看(过去 30 天)
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