Filter löschen
Filter löschen

Extracting and formatting data from a txt like file

1 Ansicht (letzte 30 Tage)
Bernard
Bernard am 19 Aug. 2013
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?

Akzeptierte Antwort

Cedric
Cedric am 19 Aug. 2013
Bearbeitet: Cedric am 19 Aug. 2013
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 Kommentare
Bernard
Bernard am 19 Aug. 2013
I forgot to mention the file has a lot more random text and such so there are headers and other numerical data
Cedric
Cedric am 19 Aug. 2013
Bearbeitet: Cedric am 19 Aug. 2013
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by