help-octave
[Top][All Lists]
Advanced

[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: Dii
Subject: Re: Help me to extract the part of Text file to another format
Date: Sat, 12 Mar 2016 10:05:34 -0800 (PST)

mjayiitk2013 wrote
> Hi all,
> 
> I have a text file 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
> I want to extract the second column between "offset 0" to "X-Ray" in a row
> then another piece in another row
> 
> I have similar files a lot so i want to change all in this manner
> Please guide me to make a script for it

This page is you friend now, it provides info on relevant functions:
https://www.gnu.org/software/octave/doc/interpreter/Line_002dOriented-Input.html
I'll provide some example, but someone who actually knows your goals might
find a room for a few improvements to those. Also, there's also page for
complete file readings which theoretically should be faster, but I'm not
experienced with those.

Example:

    f=fopen(220.TXT);
    s=fgetl(f);%this provides you first line
    while s~=-1 %until the end of file
        if strmatch('offset',s);
        %here you do your data processing for ex:
        %WARNING LAZYCODE
            s=fgetl(f);
            i=1;
            while ~strmatch("X-ray",s)
                 [~,~,~,~,row{i}]=regexp(s, '\d (\d+)' , 'once' );%quotes in
regexp are single
                 i=i+1; s=fgetl(f);
                 %by the end of this cycle you'll have cell array of cell(1)
arrays of strings representing your   
                 %numbers
            end
        end
        s=fgetl(f);%this provides you next line line
    end
    fclose(f);
    




--
View this message in context: 
http://octave.1599824.n4.nabble.com/Help-me-to-extract-the-part-of-Text-file-to-another-format-tp4675435p4675439.html
Sent from the Octave - General mailing list archive at Nabble.com.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]