How do I read data after a specific string?
Afficher commentaires plus anciens
I have three sets of data: raw, average, and median. They are all in different .txt files and have 1024 data points with its x, y, z, etc.
Now I'm trying to get the data into the workspace so I can run some analysis on it but I don't know how to make a code work for the three different sets. They all start with machine and measurement specifications like date, voltage, scaling, etc. for 30-60 lines and then the data starts after three *.
Example median data_date_time
Comments and machine specifications:
... (for 30-60 lines)
Data: x, y, z, etc.
***
-8.710498,-0.039902,0.021362,-0.028009,-0.006365
-8.689081,-0.022736,0.006943,-0.027939,-0.006338
-8.667665,-0.014191,0.031357,-0.027870,-0.006311
-8.646248,-0.018768,0.033569,-0.027800,-0.006283
-8.624832,-0.029907,0.025787,-0.027731,-0.006256
-8.603415,-0.037689,0.003281,-0.027661,-0.006228
-8.581999,-0.048065,-0.009613,-0.027591,-0.006200
... (for 1024 points).
I've read some comparable questions in the forums and tried to format the answers to my problem but I've had no luck. What I do understand is that I have to read the file from the beginning, skip all 30-60 lines before *s and read the data after but I don't know how.
Réponse acceptée
Plus de réponses (1)
If you don't know the number of header lines or they're variable as I gather they are, two choices:
- The easy route...see if importdata will "just work" and find the data you're looking for automagically doing the job for you for free, or
- Scan the file line-by-line until you find the magic string, then read the rest. This isn't as hard as it sounds...
fid=fopen('yourfile','r'); % open the file; be sure to add the error-checking code
l=fgetl(fid); % read first record
while ~strcmp(l,'***') % loop until find the magic record
l=fgetl(fid);
end
data=cell2mat(textscan(fid,'','collectoutput',1)); % read, convert cell to double array
fid=fclose(fid); % done with file, go on...
Now do whatever you need with the array.
2 commentaires
Jason Jeong
le 24 Avr 2017
dpb
le 24 Avr 2017
Ooops...left out the textscan root call...
Catégories
En savoir plus sur Text Data Preparation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!