Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to import values in specific area in a text file

Subject: how to import values in specific area in a text file

From: akihiro mizutani

Date: 7 Sep, 2009 07:40:18

Message: 1 of 3

Hello.

I would like to import values from a text file like this;

##############
ActiveX Version: 2009.0414

[File Info]
Filename: C:\Document\file.txt
Samplerate [Hz]: 10,000,000
Sets on File: 242
Samples / Set: 1024

[Set Info]
Set: 1
Index: 1
Time: 11 13:40:42 399.4114
Channel: 2
PreTrigger Samples: 200
Scaled Data [mV]

0
0
0.00152587890625
0.0030517578125
0.0030517578125
0.0030517578125
...
##############

What I need is time information in line 12.
And then, I would like to convert hh:mm:ss data into second.
So I think the time data, 13, 40, 42 should be imported separately.

I have tried to do so using 'textread' but I can't import only the value of 13:40:42.
I would be grateful if you help me.

Subject: how to import values in specific area in a text file

From: Rune Allnor

Date: 7 Sep, 2009 08:26:53

Message: 2 of 3

On 7 Sep, 09:40, "akihiro mizutani" <aki.mizut...@gmail.com> wrote:
> Hello.
>
> I would like to import values from a text file like this;
>
> ##############
> ActiveX Version: 2009.0414
>
> [File Info]
> Filename: C:\Document\file.txt
> Samplerate [Hz]: 10,000,000
> Sets on File: 242
> Samples / Set: 1024
>
> [Set Info]
> Set: 1
> Index: 1
> Time:  11 13:40:42 399.4114
> Channel: 2
> PreTrigger Samples: 200
> Scaled Data [mV]
>
> 0
> 0
> 0.00152587890625
> 0.0030517578125
> 0.0030517578125
> 0.0030517578125
> ...
> ##############
>
> What I need is time information in line 12.
> And then, I would like to convert hh:mm:ss data into second.
> So I think the time data, 13, 40, 42 should be imported separately.
>
> I have tried to do so using 'textread' but I can't import only the value of 13:40:42.
> I would be grateful if you help me.

First of all, find something unique that identifies the
file you want. It might be the line number, if the file
format is static, or it can be the format of the line.

The format of the line is the safest method to use, since
it is robust with respect to changes in the file format.

The way to search for the line pattern is to use a regulcar
expression:

rex = '^Time:.*\d{2}:\d{2}:\d{2}';
regexp(s,rex)

This regular expression mathches all lines that start with
'Time:' and contains three groups of two digits separated
by colon, with any number of other characters inbetween.

You use it something like this:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    rex = '^Time:.*\d{2}:\d{2}:\d{2}';
    fid = fopen(filename,'rt');
    idx = [];
    while(~feof(fid) && isempty(idx))
        s = fgetl(feof);
        idx = regexpi(s,rex);
        if (~isempty(idx))
            % extract info from line
        end
    end
    fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

This might seem a lot more involved than TEXTREAD,
but it's not. Something like is goes on inside TEXTREAD
anyway. The above is, in fact, a lot faster than TEXTREAD,
since it terminates once the time stam has been found,
whereas TEXTREAD reads all the file.

We can do even better: We can use 'named capture' to
extract the individual groups of digits at the same
time we check the line format. That way we don't need
to do the same work twice:

rex = '^Time:.*(?<hour>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})';
[tokens,names]=regexp(s,rex,'tokens','names')


tokens =

    {1x3 cell}


names =

       hour: '13'
    minutes: '40'
    seconds: '42'

So now you can access the 'names' struct to find the
data items you want.

You could have used a simple SSCANF statement in the line
you found with the first regular expression[*], but this
method is far more flexible.

Rune

[*] You still need to use SSCANF or INT2STR, since 'names'
    contains strings, not numbers.

Subject: how to import values in specific area in a text file

From: akihiro mizutani

Date: 8 Sep, 2009 10:19:04

Message: 3 of 3

Rune Allnor <allnor@tele.ntnu.no> wrote in message <39cf8217-be07-47c6-900d-7e64240e3377@r5g2000yqi.googlegroups.com>...
> On 7 Sep, 09:40, "akihiro mizutani" <aki.mizut...@gmail.com> wrote:
> > Hello.
> >
> > I would like to import values from a text file like this;
> >
> > ##############
> > ActiveX Version: 2009.0414
> >
> > [File Info]
> > Filename: C:\Document\file.txt
> > Samplerate [Hz]: 10,000,000
> > Sets on File: 242
> > Samples / Set: 1024
> >
> > [Set Info]
> > Set: 1
> > Index: 1
> > Time: ?11 13:40:42 399.4114
> > Channel: 2
> > PreTrigger Samples: 200
> > Scaled Data [mV]
> >
> > 0
> > 0
> > 0.00152587890625
> > 0.0030517578125
> > 0.0030517578125
> > 0.0030517578125
> > ...
> > ##############
> >
> > What I need is time information in line 12.
> > And then, I would like to convert hh:mm:ss data into second.
> > So I think the time data, 13, 40, 42 should be imported separately.
> >
> > I have tried to do so using 'textread' but I can't import only the value of 13:40:42.
> > I would be grateful if you help me.
>
> First of all, find something unique that identifies the
> file you want. It might be the line number, if the file
> format is static, or it can be the format of the line.
>
> The format of the line is the safest method to use, since
> it is robust with respect to changes in the file format.
>
> The way to search for the line pattern is to use a regulcar
> expression:
>
> rex = '^Time:.*\d{2}:\d{2}:\d{2}';
> regexp(s,rex)
>
> This regular expression mathches all lines that start with
> 'Time:' and contains three groups of two digits separated
> by colon, with any number of other characters inbetween.
>
> You use it something like this:
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> rex = '^Time:.*\d{2}:\d{2}:\d{2}';
> fid = fopen(filename,'rt');
> idx = [];
> while(~feof(fid) && isempty(idx))
> s = fgetl(feof);
> idx = regexpi(s,rex);
> if (~isempty(idx))
> % extract info from line
> end
> end
> fclose(fid);
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> This might seem a lot more involved than TEXTREAD,
> but it's not. Something like is goes on inside TEXTREAD
> anyway. The above is, in fact, a lot faster than TEXTREAD,
> since it terminates once the time stam has been found,
> whereas TEXTREAD reads all the file.
>
> We can do even better: We can use 'named capture' to
> extract the individual groups of digits at the same
> time we check the line format. That way we don't need
> to do the same work twice:
>
> rex = '^Time:.*(?<hour>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})';
> [tokens,names]=regexp(s,rex,'tokens','names')
>
>
> tokens =
>
> {1x3 cell}
>
>
> names =
>
> hour: '13'
> minutes: '40'
> seconds: '42'
>
> So now you can access the 'names' struct to find the
> data items you want.
>
> You could have used a simple SSCANF statement in the line
> you found with the first regular expression[*], but this
> method is far more flexible.
>
> Rune
>
> [*] You still need to use SSCANF or INT2STR, since 'names'
> contains strings, not numbers.


Thank you very much!!
I could solve the problem somehow referring to your advise.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us