I want to find a specific text and copy rows below the text

3 views (last 30 days)
I'm a little new to Matlab so I'm not sure how to explain this in a proper manner.
I use data from a text file and convert it to a csv and then use xlsread in Matlab to import it. The first issue I have is that the time and date isn't in a conventional format so it is seen as a text making it hard to find just the date part of it.
The main issue I have is finding a certain text (let's say the date, with index n) and copying that row plus 12 rows below the "DATA:" row, for every text matching the search (as shown below).
What would be a good way to search for a date and copying rows based on which row the date is? After copying I want to create a new excel file to paste the data onto.
--------------------------------------------------------------------------------------------------------
2015-02-18-07.11.56
SETUP=1517879
LABELS:
2520150218061502,1516318,025,D322222,OIS01,A,85022,Part Change / Start-up,1245650-0001,0,0,0,0,0,0,DVS89333,NA,Pinnacle PIN_P1.voy
DATA:
1,4.27773,4.26984,4.29599,4.29913,17.47136,
2,4.26795,4.27213,4.28142,4.29163,17.49805,
3,4.26312,4.27214,4.28423,4.29859,17.50565,
4,4.26810,4.26908,4.28203,4.30279,17.44669,
5,4.27386,4.27463,4.27487,4.29476,17.43890,
6,4.26651,4.27061,4.28602,4.29881,17.44738,
7,4.26255,4.27015,4.28775,4.30516,17.38656,
8,4.27065,4.27353,4.28205,4.29811,17.26813,
9,4.27343,4.27177,4.28809,4.30305,17.43701,
10,4.26810,4.27268,4.28628,4.29812,17.50405,
11,4.27042,4.26886,4.28685,4.29842,17.43868,
12,4.26550,4.27015,4.29229,4.30419,17.54224,
--------------------------------------------------------------------------------------------------------
Thank you, any help is appreciated
  2 Comments
dpb
dpb on 25 Feb 2015
Edited: dpb on 26 Feb 2015
Are the first/last line of minus signs part of the data file or did you just use them to demarcate the data from the rest of your message?
Is the data after the LABELS: line a single record or multiple records? It doesn't seem to match up with the number of columns of DATA:
What is it you're searching for, precisely? If the records are fixed number as outlined by whatever the answers are to the above questions, it's pretty simple to write a repeating textscan loop; just need to know those answers to know exactly what that format would be.
The date string is pretty simple to decode--
>> ds='2015-02-18-07.11.56';
>> [y,m,d,h,n,s]=datevec(ds,'yyyy-mm-dd-HH.MM.SS')
y =
2015
m =
2
d =
18
h =
7
n =
11
s =
56
>>
Pdngl
Pdngl on 4 Mar 2015
Sorry about replying late, I didn't see this answer
The minus sign isn't a part of the data, I just used it to separate the data
The data after LABELS: are multiple records of what settings were used
What I'm looking for is just the 12 rows under DATA:, there are ten to hundreds of such recording and I would like to have a loops that finds data from a certain date and copies the data along with the 12 rows of data underneath it.
Currently I do this manually and it takes a while

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 4 Mar 2015
A simple way but possibly not the fastest:
targetdate = '2015-02-18';
alllines = strsplit(fileread('somefile.txt'), '\n');
datastarts = find(strncmp(alllines, 'DATA:', numel('DATA:')));
dateline = find(strncmp(alllines, targetdate, numel(targetdate)));
if numel(dateline) == 0
error('target date not found');
elseif numel(dateline) > 1
error('several target dates found');
end
datastart = find(datastarts > dateline, 1) + 1;
data = alllines(datastart + 1 : datastart + 12);

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!