Search and Parse Variable Length Text Files to Arrays?

3 views (last 30 days)
Hi Folks,
I could really use some help searching and converting (to array and/or vectors) a particular (but variable length) piece of text that appears in multiple text files.
Here's what a typical file looks like:
Start Time: 0.0
Time Elapsed: 600.032146474
Initial Position: [-0.17248877882957458, 1.2094972133636475, -0.058800775557756424]
Final Position: [4.5681076049804687, 1.6064592599868774, -4.8270955085754395]
Distance Traveled: 312.929245491
Relative Distance: 6.72382991569
Initial Orientation: [-9.5176773071289062, 3.2656052112579346, 0.30539315938949585]
Final Orientation: [49.040523529052734, -0.74396026134490967, -3.8589010238647461]
Total Angular Movement: 40838.0665952
Relative Change in Rotation: 58.5582008362
Portals Breached:
['sinkPortal', 82.382585985090287]
['cactusPortal', 167.32241884094208]
['bookcasePortal', 262.14078493216317]
['cactusPortal', 269.48917152878369]
['sinkPortal', 383.20171439386849]
['bookcasePortal', 411.92835136867956]
['wellPortal', 457.79343079916583]
['sinkPortal', 466.6405778527718]
['bookcasePortal', 494.15008779048731]
['bookcasePortal', 569.05743781681747]
Objects Visited:
['home', 0.0088714487455838764]
['left home', 20.152287930449265]
['moon', 27.149798996799877]
. . .
What I want to get from these files is just the information that you find between "Portals Breached" and "Objects Visited". The number of entries between those two bits of text varies from file to file. Here's what I want (e.g. two column vectors or one Nx2 array):
sinkPortal 82.382585985090287
cactusPortal 167.32241884094208
bookcasePortal 262.14078493216317
cactusPortal 269.48917152878369
sinkPortal 383.20171439386849
bookcasePortal 411.92835136867956
wellPortal 457.79343079916583
sinkPortal 466.6405778527718
bookcasePortal 494.15008779048731
bookcasePortal 569.05743781681747
I should note that the number of lines in the text file before "Portals Breached" appears is standard across all the separate text files.
Thanks! Jon

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 27 Sep 2011
Copy your text to file test.txt and run the following code. Note that you may lose precision in NumData comparing to the digits in Data.
fid=fopen('test.txt');
Str=textscan(fid,'%s','delimiter',',');
fclose(fid);
Str=Str{1};
Ind=find(strcmp(Str,'Portals Breached:'));
Str(1:Ind)=[];
Ind=find(strcmp(Str,'Objects Visited:'));
Str(Ind:end)=[];
Name=Str(1:2:end);
Data=Str(2:2:end);
Name=strrep(Name,'[''','');
Name=strrep(Name,'''','')
Data=strrep(Data,']','');
NumData=str2double(Data)
Name =
'sinkPortal'
'cactusPortal'
'bookcasePortal'
'cactusPortal'
'sinkPortal'
'bookcasePortal'
'wellPortal'
'sinkPortal'
'bookcasePortal'
'bookcasePortal'
NumData =
82.3826
167.3224
262.1408
269.4892
383.2017
411.9284
457.7934
466.6406
494.1501
569.0574

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!