Effacer les filtres
Effacer les filtres

Extracting and formatting data from a txt like file

1 vue (au cours des 30 derniers jours)
Bernard
Bernard le 19 Août 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?

Réponse acceptée

Cedric
Cedric le 19 Août 2013
Modifié(e) : Cedric le 19 Août 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 commentaires
Bernard
Bernard le 19 Août 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 le 19 Août 2013
Modifié(e) : Cedric le 19 Août 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by