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:
read file with unhomogen structure

Subject: read file with unhomogen structure

From: edward kabanyas

Date: 12 Feb, 2008 16:28:01

Message: 1 of 13

Dear All,
I have problem in reading file containing string or
character in my file. The following are the data example:

11:48:00 -> 11:49:00 0.00 -> 0.25 mm n= 0.0000000 /m3mm
11:48:00 -> 11:49:00 0.25 -> 0.50 mm n= 0.0000000 /m3mm
11:48:00 -> 11:49:00 0.50 -> 0.75 mm n= 0.0000000 /m3mm
11:48:00 -> 11:49:00 0.75 -> 1.00 mm n= 1.8491934 /m3mm
rainrate =0.001686 mm/hr
11:49:00 -> 11:50:00 0.00 -> 0.25 mm n= 0.0000000 /m3mm
11:49:00 -> 11:50:00 0.25 -> 0.50 mm n= 0.0000000 /m3mm
11:49:00 -> 11:50:00 0.50 -> 0.75 mm n= 2.6686186 /m3mm
11:49:00 -> 11:50:00 0.75 -> 1.00 mm n= 0.0000000 /m3mm
rainrate =0.000455 mm/hr
11:50:00 -> 11:51:00 0.00 -> 0.25 mm n= 0.0000000 /m3mm
11:50:00 -> 11:51:00 0.25 -> 0.50 mm n= 0.0000000 /m3mm
11:50:00 -> 11:51:00 0.50 -> 0.75 mm n= 2.3139154 /m3mm
11:50:00 -> 11:51:00 0.75 -> 1.00 mm n= 3.5420286 /m3mm
rainrate =0.004181 mm/hr

Desired new file structure:
11:48:00 0.0000000 0.0000000 0.0000000 1.8491934 0.001686
11:49:00 0.0000000 0.0000000 2.6686186 0.0000000 0.000455
11:50:00 0.0000000 0.0000000 2.3139154 3.5420286 0.004181

I really need your help. I want to pick the value of last
coulumn (value of after n), rainrate value and time only and
putting them in new file with structure as desired new file.

That's my question, thanks very much for your best help

Best regards;
Edward

Subject: read file with unhomogen structure

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 12 Feb, 2008 17:34:52

Message: 2 of 13

In article <fosheh$4ec$1@fred.mathworks.com>,
edward kabanyas <djuky_hmi@yahoo.com> wrote:

>I have problem in reading file containing string or
>character in my file. The following are the data example:

You did not need to repost your question so soon after posting
it the first time.


>11:48:00 -> 11:49:00 0.00 -> 0.25 mm n= 0.0000000 /m3mm
>11:48:00 -> 11:49:00 0.25 -> 0.50 mm n= 0.0000000 /m3mm
>11:48:00 -> 11:49:00 0.50 -> 0.75 mm n= 0.0000000 /m3mm
>11:48:00 -> 11:49:00 0.75 -> 1.00 mm n= 1.8491934 /m3mm
>rainrate =0.001686 mm/hr
>11:49:00 -> 11:50:00 0.00 -> 0.25 mm n= 0.0000000 /m3mm
>11:49:00 -> 11:50:00 0.25 -> 0.50 mm n= 0.0000000 /m3mm
>11:49:00 -> 11:50:00 0.50 -> 0.75 mm n= 2.6686186 /m3mm
>11:49:00 -> 11:50:00 0.75 -> 1.00 mm n= 0.0000000 /m3mm
>rainrate =0.000455 mm/hr
>11:50:00 -> 11:51:00 0.00 -> 0.25 mm n= 0.0000000 /m3mm
>11:50:00 -> 11:51:00 0.25 -> 0.50 mm n= 0.0000000 /m3mm
>11:50:00 -> 11:51:00 0.50 -> 0.75 mm n= 2.3139154 /m3mm
>11:50:00 -> 11:51:00 0.75 -> 1.00 mm n= 3.5420286 /m3mm
>rainrate =0.004181 mm/hr

>Desired new file structure:
>11:48:00 0.0000000 0.0000000 0.0000000 1.8491934 0.001686
>11:49:00 0.0000000 0.0000000 2.6686186 0.0000000 0.000455
>11:50:00 0.0000000 0.0000000 2.3139154 3.5420286 0.004181

>I want to pick the value of last
>coulumn (value of after n), rainrate value and time only and
>putting them in new file with structure as desired new file.

The format for each group is
'%s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s%*2c%f %*s'

Another way of writing that would be,

['%s %*s %*s %*s %*s %*s %*s %*s %f %*s', ...
 repmat('%*s %*s %*s %*s %*s %*s %*s %*s %f %*s', 1, 3), ...
 '%*s%*2c%f %*s']
--
   "Beware of bugs in the above code; I have only proved it correct,
   not tried it." -- Donald Knuth

Subject: read file with unhomogen structure

From: edward kabanyas

Date: 12 Feb, 2008 18:15:04

Message: 3 of 13

Hi Roberson!

Thanks for your reply.

However, I still do not understand well what you mean.
Do you mean like this ?

fid=fopen('V07188_m.dsd'); % V07188_m.dsd is input file with
many rows with structure as I sent before.
========================00
r = 1;
while 1
    a=textread(fid,'%s %*s %*s %*s %*s %*s %*s %*s %f %*s
 %*s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s %*s %*s %*s %*s
%*s %*s %*s %f %*s %*s %*s %*s %*s %*s %*s %*s %*s %f %*s
%*s%*2c%f %*s');
    tline = fgetl(fid);
    if ~ischar(tline), break, end
    r = r+1;
  
end
    
fclose(fid);

Subject: read file with unhomogen structure

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 12 Feb, 2008 18:24:02

Message: 4 of 13

In article <fosnn8$j3b$1@fred.mathworks.com>,
edward kabanyas <djuky_hmi@yahoo.com> wrote:

>However, I still do not understand well what you mean.
>Do you mean like this ?

>fid=fopen('V07188_m.dsd'); % V07188_m.dsd is input file with
>many rows with structure as I sent before.
>========================00
>r = 1;
>while 1
> a=textread(fid,'%s %*s %*s %*s %*s %*s %*s %*s %f %*s
> %*s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s %*s %*s %*s %*s
>%*s %*s %*s %f %*s %*s %*s %*s %*s %*s %*s %*s %*s %f %*s
>%*s%*2c%f %*s');
> tline = fgetl(fid);
> if ~ischar(tline), break, end
> r = r+1;
>
>end

>fclose(fid);

fid=fopen('V07188_m.dsd');
if fid < 0
  error('could not open file');
end
[timest,v1,v2,v3,v4,rr] = textread(fid,'%s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s%*2c%f %*s');
fclose(fid);

Then timest will contain the timestamps, [v1, v2, v3, v4] will
contain the four end-of-line values, and rr will contain the rain rates
from the lines at the end of each block.

--
  "When a scientist is ahead of his times, it is often through
   misunderstanding of current, rather than intuition of future truth.
   In science there is never any error so gross that it won't one day,
   from some perspective, appear prophetic." -- Jean Rostand

Subject: read file with unhomogen structure

From: edward kabanyas

Date: 12 Feb, 2008 19:06:02

Message: 5 of 13

Hi Roberson;

Thanks again for your quick reply.I just make copy of your
code, but it doest not run.

final.m
=========
fid=fopen('V07188_m.dsd');
if fid < 0
  error('could not open file');
end
[timest,v1,v2,v3,v4,rr] = textread(fid,'%s %*s %*s %*s %*s
%*s %*s %*s %f %*s %*s %*s %*s %*s %*s %*s %*s %*s %f %*s
%*s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s %*s %*s %*s %*s
%*s %*s %*s %f %*s %*s%*2c%f %*s');
fclose(fid);
==========

Some error like this occur:

??? Error using ==> exist
The first input to exist is a string.

Error in ==> textread at 161
if (exist(varargin{1}) ~= 2 |
exist(fullfile(cd,varargin{1})) ~= 2) &
~isempty(which(varargin{1}))

Error in ==> final at 5
[timest,v1,v2,v3,v4,rr] = textread(fid,'%s %*s %*s %*s %*s
%*s %*s %*s %f %*s %*s %*s %*s %*s %*s %*s %*s %*s %f %*s
%*s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s %*s %*s %*s %*s
%*s %*s %*s %f %*s %*s%
>>


I don't know why, but anyway thanks a lot for your help

Best regards;
Edward

Subject: read file with unhomogen structure

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 12 Feb, 2008 19:17:36

Message: 6 of 13

In article <fosqmq$pn$1@fred.mathworks.com>,
edward kabanyas <djuky_hmi@yahoo.com> wrote:

>Thanks again for your quick reply.I just make copy of your
>code, but it doest not run.

>final.m
>=========
>fid=fopen('V07188_m.dsd');
>if fid < 0
> error('could not open file');
>end
>[timest,v1,v2,v3,v4,rr] = textread(fid,'%s %*s %*s %*s %*s
>%*s %*s %*s %f %*s %*s %*s %*s %*s %*s %*s %*s %*s %f %*s
>%*s %*s %*s %*s %*s %*s %*s %*s %f %*s %*s %*s %*s %*s %*s
>%*s %*s %*s %f %*s %*s%*2c%f %*s');
>fclose(fid);
>==========

>Some error like this occur:

>??? Error using ==> exist
>The first input to exist is a string.

Ah, right. Okay then everything gets condensed
into one call, with no fopen or fclose:

[timest,v1,v2,v3,v4,rr] = textread('V07188_m.dsd', ...
  ['%s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s%*2c%f %*s'] );

textread() expects a file name, not a fid.


The alternative would be to use the same fopen/fclose type
of logic, but change the textread to textscan; if you
do that, though, then instead of seperate output variables
you would need to change to a cell array:

fid = fopen(V07188_m.dsd');
if fid < 0
  error('could not open file');
end
C = textscan(fid, ...
  ['%s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s%*2c%f %*s'] );
fclose(fid);
--
  "The art of storytelling is reaching its end because the epic
  side of truth, wisdom, is dying out." -- Walter Benjamin

Subject: read file with unhomogen structure

From: edward kabanyas

Date: 12 Feb, 2008 19:42:01

Message: 7 of 13


Hi Reberson;

thanks again for your reply.

but it still does not run...he2x
I just make copy like this:
[timest,v1,v2,v3,v4,rr] = textread('V07188_m.dsd', ...
  ['%s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
   '%*s%*2c%f %*s']);
but error like this occur:
??? Trouble reading floating point number from file (row 2,
field 9) ==> /m3mm\n

Error in ==> textread at 177
[varargout{1:nlhs}]=dataread('file',varargin{:});

Error in ==> juki at 1
[timest,v1,v2,v3,v4,rr] = textread('V07188_m.dsd', ...

>>

Do you see any solution? Again, thannks for your nice help

regards;
Edward

Subject: read file with unhomogen structure

From: Ashish Uthama

Date: 13 Feb, 2008 17:31:46

Message: 8 of 13

Hi Edward,

I would think you got great help from Roberson.

 From the error message

     Trouble reading floating point number from file (row 2, field 9)

which according to the data from your first posting indicates that you
need to tweak the format string (the '%' chars like %s, %*s) to match
your data. As the error indicates, when it reaches the 9th element of
the formating string, it has already read in the number and is trying
to interpret /m3mm as a floating point number.

Maybe you could read more about the formating here (it the link wraps,
make sure to join it up..)


http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access=
/helpdesk/help/techdoc/ref/sscanf.html


Ashish

On Tue, 12 Feb 2008 14:42:01 -0500, edward kabanyas <djuky_hmi@yahoo.com=
> =

wrote:

>
> Hi Reberson;
>
> thanks again for your reply.
>
> but it still does not run...he2x
> I just make copy like this:
> [timest,v1,v2,v3,v4,rr] =3D textread('V07188_m.dsd', ...
> ['%s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
> '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
> '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
> '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
> '%*s%*2c%f %*s']);
> but error like this occur:
> ??? Trouble reading floating point number from file (row 2,
> field 9) =3D=3D> /m3mm\n
>
> Error in =3D=3D> textread at 177
> [varargout{1:nlhs}]=3Ddataread('file',varargin{:});
>
> Error in =3D=3D> juki at 1
> [timest,v1,v2,v3,v4,rr] =3D textread('V07188_m.dsd', ...
>
>>>
>
> Do you see any solution? Again, thannks for your nice help
>
> regards;
> Edward
>
>



-- =

Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Subject: read file with unhomogen structure

From: us

Date: 13 Feb, 2008 18:18:07

Message: 9 of 13

"edward kabanyas":
<SNIP formatted input dissection evergreen...

one of the many solutions is outlined below
- assume your file anatomy as shown!

% the data
     fnam='foo.txt'; % <- your file name...
% the engine
     s=textread(fnam,'%s','delimiter','\n');
     tmpl='rainrate';
     ix=false(size(s,1),1);
     ix(strncmp(tmpl,s,numel(tmpl)))=true;
     ix=find(ix);
     m=regexp(s(ix),['(\d+\.\d+)'],'match');
     m=[m{:}].';
     n=regexp(s,'^(\d+:\d+:\d+)|\d+\.\d+(?=( /m))',...
                'match');
     n=reshape([n{:}],2,[]).';
     [nu,nx,nx]=unique(n(:,1));
     r=arrayfun(@(x) [nu(x),n(nx==x,2).',m(x)],...
                1:numel(m),'uni',false).';
% the result
     r=cat(1,r{:}).';
     r=sprintf([repmat('%s ',1,6),'\n'],r{:});
     disp(r);
%{
11:48:00 0.0000000 0.0000000 0.0000000 1.8491934 0.001686
11:49:00 0.0000000 0.0000000 2.6686186 0.0000000 0.000455
11:50:00 0.0000000 0.0000000 2.3139154 3.5420286 0.004181
%}

us

Subject: read file with unhomogen structure

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 13 Feb, 2008 19:14:09

Message: 10 of 13

In article <fossq9$6fj$1@fred.mathworks.com>,
edward kabanyas <djuky_hmi@yahoo.com> wrote:

>but it still does not run...he2x
>I just make copy like this:
>[timest,v1,v2,v3,v4,rr] = textread('V07188_m.dsd', ...
> ['%s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
> '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
> '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
> '%*s %*s %*s %*s %*s %*s %*s %*s %f %*s ', ...
> '%*s%*2c%f %*s']);
>but error like this occur:
>??? Trouble reading floating point number from file (row 2,
>field 9) ==> /m3mm\n

In the data you show, /m3mm is the 10th field, not the 9th.
That suggests that the data you have might not be exactly the
same as what you posted.

--
  "Style is instinctive and few achieve it in a notable degree. Its
   development is not hastened by instruction. It comes or it doesn't.
   It will take care of itself." -- Walter J. Phillips

Subject: read file with unhomogen structure

From: edward kabanyas

Date: 14 Feb, 2008 15:14:04

Message: 11 of 13

Dear Us,

Thanks very much it works well.
However, if want do exclude (:) in the output, for example, now
11:48:00 0.0000000 0.0000000 0.0000000 1.8491934 0.001686

It will be helpful for me if the output like this:
11 48 00 0.0000000 0.0000000 0.0000000 1.8491934 0.001686

Because, I'm still beginner in Matlab, I don't like any
character in my data file to make easier analysis.

Again, thanks very much

Edward




"us " <us@neurol.unizh.ch> wrote in message
<fovc8v$hr7$1@fred.mathworks.com>...
> "edward kabanyas":
> <SNIP formatted input dissection evergreen...
>
> one of the many solutions is outlined below
> - assume your file anatomy as shown!
>
> % the data
> fnam='foo.txt'; % <- your file name...
> % the engine
> s=textread(fnam,'%s','delimiter','\n');
> tmpl='rainrate';
> ix=false(size(s,1),1);
> ix(strncmp(tmpl,s,numel(tmpl)))=true;
> ix=find(ix);
> m=regexp(s(ix),['(\d+\.\d+)'],'match');
> m=[m{:}].';
> n=regexp(s,'^(\d+:\d+:\d+)|\d+\.\d+(?=( /m))',...
> 'match');
> n=reshape([n{:}],2,[]).';
> [nu,nx,nx]=unique(n(:,1));
> r=arrayfun(@(x) [nu(x),n(nx==x,2).',m(x)],...
> 1:numel(m),'uni',false).';
> % the result
> r=cat(1,r{:}).';
> r=sprintf([repmat('%s ',1,6),'\n'],r{:});
> disp(r);
> %{
> 11:48:00 0.0000000 0.0000000 0.0000000 1.8491934 0.001686
> 11:49:00 0.0000000 0.0000000 2.6686186 0.0000000 0.000455
> 11:50:00 0.0000000 0.0000000 2.3139154 3.5420286 0.004181
> %}
>
> us

Subject: read file with unhomogen structure

From: us

Date: 14 Feb, 2008 17:51:02

Message: 12 of 13

"edward kabanyas":
<SNIP on removing unwanted <char>s...

one of the solutions

% assume you got the <r> from the last snippet
%{
r =
11:48:00 0.0000000 0.0000000 0.0000000 1.8491934 0.001686
11:49:00 0.0000000 0.0000000 2.6686186 0.0000000 0.000455
11:50:00 0.0000000 0.0000000 2.3139154 3.5420286 0.004181
%}
% now
     r=strrep(r,':',' ')
%{
r =
11 48 00 0.0000000 0.0000000 0.0000000 1.8491934 0.001686
11 49 00 0.0000000 0.0000000 2.6686186 0.0000000 0.000455
11 50 00 0.0000000 0.0000000 2.3139154 3.5420286 0.004181
%}

us

Subject: read file with unhomogen structure

From: edward kabanyas

Date: 21 Feb, 2008 11:27:01

Message: 13 of 13

Hi Us;

> r=strrep(r,':',' ')

It runs well, and again, thanks very much.

Cheers
jUKy

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