[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Help me to extract the part of Text file to another format
From: |
Ben Abbott |
Subject: |
Re: Help me to extract the part of Text file to another format |
Date: |
Sat, 12 Mar 2016 20:13:30 -0500 |
> On Mar 12, 2016, at 7:22 AM, mjayiitk2013 <address@hidden> wrote:
>
> Hi all,
>
> I have a text file 40%ROL~1.TXT
> <http://octave.1599824.n4.nabble.com/file/n4675435/40%25ROL%7E1.TXT> who
> has pattern of this kind
>
> info info info
> info X X X
> Info
> offset 0
> 1 344
> 2 43432
> 4 34234
> 3 4323
> X-ray
> Info
> Info
> offset 0
> 1 3441
> 2 43431
> 4 34231
> 3 4321
> X-ray
> ....
> ....
> I want to extract the second column between "offset 0" to "X-Ray" in a row
> then another piece in another row
> like
> 344 43432 34234 4323
> 3441 43431 34231 4321
>
> I have similar files a lot so i want to change all in this manner
> Please guide me to make a script for it
I process a lot of text data at work. Below is an approach I like. It places
each range of data in the structure array, dat.
txt = fileread ("220.txt");
txt = strsplit (txt, "\n");
ok = @(c) cellfun (@(x)!isempty(x), c, "uniformoutput", true);
txt(!ok(txt)) = [];
header_begin = find (ok (regexpi (txt, "^X-Ray.*")));
header_end = find (ok (regexpi (txt, "^Offset.*")));
dat = struct ();
for n = numel(header_begin):-1:1
for m = header_begin(n):header_end(n)
str = strsplit (txt{m}, '\s', ...
"delimitertype", "regularexpression", ...
"collapsedelimiters", true);
dat(n).(str{1}) = str2double (str{2});
if (isnan (dat(n).(str{1})))
dat(n).(str{1}) = str{2};
end
end
num = round ((dat(n).Stop - dat(n).Start) / dat(n).Step);
m = header_end(n) + (1:num);
dat(n).data = vertcat (cellfun (@str2num, txt(m), "uniformoutput", false){:});
end
To collect all the data in a single 2D array ...
all_data = vertcat (cellfun (@(x)x(:,2).', {dat.data}, “uniformoutput",
false){:})
Ben