Thread Subject: Importing data from txt file

Subject: Importing data from txt file

From: Mike Giordano

Date: 10 Jun, 2009 14:42:01

Message: 1 of 2

Hello all,

I am trying to write an m-file to import data that is set up in the following way:

{Some line with comments}

#data
#data
#data

{Line with comments}

#data
.....

The number of lines of "data" are different throughout the file so I was thinking the best way to do this would be to write a loop that puts everything between blank lines into a new array.

My big question is: is there a way to do this in matlab? How do I read a blank line?

Thanks in advance for any help.

Subject: Importing data from txt file

From: John

Date: 12 Jun, 2009 16:15:03

Message: 2 of 2

If you are writing a loop, check for blank lines by using the strtrim and isempty function. e.g if each line is read in variable tline (say)

if isempty(strtrim(tline))
 %% blank line so skip
 continue;
else
%% add to array
end

using strtrim in case blank line is not really blank but has white space characters so strtrim returns an empty character string.

Alternative to writing loop would be trying textread function. If you comments are of matlab, shell, c, or C++ style ..textread has a 'commenstyle' parameter you can specify which will make it skip all the lines that begin with the comment character. Look at help textread - its a useful function.


~S
"Mike Giordano" <sakar321@gmail.com> wrote in message <h0ognp$rg6$1@fred.mathworks.com>...
> Hello all,
>
> I am trying to write an m-file to import data that is set up in the following way:
>
> {Some line with comments}
>
> #data
> #data
> #data
>
> {Line with comments}
>
> #data
> .....
>
> The number of lines of "data" are different throughout the file so I was thinking the best way to do this would be to write a loop that puts everything between blank lines into a new array.
>
> My big question is: is there a way to do this in matlab? How do I read a blank line?
>
> Thanks in advance for any help.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
strtrim John 12 Jun, 2009 12:19:08
isempty John 12 Jun, 2009 12:19:08
textread John 12 Jun, 2009 12:19:08
rssFeed for this Thread

Contact us at files@mathworks.com