Thread Subject: read

Subject: read

From: nachiketha Kukkady

Date: 17 Aug, 2009 01:48:01

Message: 1 of 9

Hi....
I've a matlab file which contains few numbers(float) in about 75 lines...
how do i read a individual line so that i can later process with these obtained numbers from some particular lines...
i did try with fgetl but that gives only the first line.. or maybe i couldn't use it properly....
could somebody help me with this??
Thank you in advance....

Subject: read

From: dpb

Date: 17 Aug, 2009 01:58:13

Message: 2 of 9

nachiketha Kukkady wrote:
> Hi.... I've a matlab file which contains few numbers(float) in about
> 75 lines... how do i read a individual line so that i can later
> process with these obtained numbers from some particular lines... i
> did try with fgetl but that gives only the first line.. or maybe i
> couldn't use it properly.... could somebody help me with this?? Thank
> you in advance....

Typically fgetl() is used in a loop.

Alternatively, w/ ML it may be simpler and just as quick to read the
whole array and then discard the portion you don't want.

--

Subject: read

From: Skeptic

Date: 17 Aug, 2009 02:03:39

Message: 3 of 9

On Aug 16, 9:48 pm, "nachiketha Kukkady" <nachike...@in.com> wrote:
> Hi....
> I've a matlab file which contains few numbers(float) in about 75 lines...
> how do i read a individual line so that i can later process with these obtained numbers from some particular lines...
> i did try with fgetl but that gives only the first line.. or maybe i couldn't use it properly....
> could somebody help me with this??
> Thank you in advance....

-------------------------------------------------------------------------
Yeah, that's probably it (you didn't use it properly) - keep trying
(or else show us your data file and your code). You can also try
other things like fread(), fscanf(), or similar functions.

Subject: read

From: nachiketha Kukkady

Date: 17 Aug, 2009 02:17:04

Message: 4 of 9

dpb <none@non.net> wrote in message <h6adn3$mg7$1@news.eternal-september.org>...
> nachiketha Kukkady wrote:
> > Hi.... I've a matlab file which contains few numbers(float) in about
> > 75 lines... how do i read a individual line so that i can later
> > process with these obtained numbers from some particular lines... i
> > did try with fgetl but that gives only the first line.. or maybe i
> > couldn't use it properly.... could somebody help me with this?? Thank
> > you in advance....
>
> Typically fgetl() is used in a loop.
>
> Alternatively, w/ ML it may be simpler and just as quick to read the
> whole array and then discard the portion you don't want.
>
> --

Thank you for that reply....
Yeah i've already the whole page or file containing those numbers....
my file is something like this.....
259.975 262.41
269.273 338.54
276.828 369.921
287.87 404.79
306.466 435.59
330.874 461.161
364.581 480.338.............
i will be needing line 2, line 5 and so on for further calculations....
As i said i've read the file using fread command.... but i don't know how exactly to remove the lines which i don't need.....
thank you....

Subject: read

From: arun

Date: 17 Aug, 2009 02:33:25

Message: 5 of 9

On Aug 17, 4:17 am, "nachiketha Kukkady" <nachike...@in.com> wrote:
> dpb <n...@non.net> wrote in message <h6adn3$mg...@news.eternal-september.org>...
> > nachiketha Kukkady wrote:
> > > Hi.... I've a matlab file which contains few numbers(float) in about
> > > 75 lines... how do i read a individual line so that i can later
> > > process with these obtained numbers from some particular lines... i
> > > did try with fgetl but that gives only the first line.. or maybe i
> > > couldn't use it properly.... could somebody help me with this?? Thank
> > > you in advance....
>
> > Typically fgetl() is used in a loop.
>
> > Alternatively, w/ ML it may be simpler and just as quick to read the
> > whole array and then discard the portion you don't want.
>
> > --
>
> Thank you for that reply....
> Yeah i've already the whole page or file containing those numbers....
> my file is something like this.....
> 259.975 262.41
> 269.273 338.54
> 276.828 369.921
> 287.87 404.79
> 306.466 435.59
> 330.874 461.161
> 364.581 480.338.............
> i will be needing line 2, line 5 and so on for further calculations....
> As i said i've read the file using fread command.... but i don't know how exactly to remove the lines which i don't need.....
> thank you....  

You have the whole matrix here. you can just access the essential
entries then.

fn = <path to file>';
fid = fopen(fn, 'r');
%preallocate variable if you know the number of rows and columns
a = cell(6,2); % file contains first six lines of the sample you
posted above, with 2 columns.
i=1;
while ~feof(fid),
    a(i,:) = regexp(fgetl(fid),'\s', 'split'); %check for space.
    i = i + 1;
end
a= cellfun(@str2num, a, 'uni', false); % conver to double array.
fclose(fid);

best, arun.

Subject: read

From: nachiketha Kukkady

Date: 17 Aug, 2009 03:00:19

Message: 6 of 9

thank you....
but this is doing the same work which is being done by fread right????
what i need is get some required lines like 20th, 34th,56th and so on and finally do some processings with it.... i don't want to use the loop here...
So what can i do now????

Subject: read

From: arun

Date: 17 Aug, 2009 03:26:07

Message: 7 of 9

On Aug 17, 5:00 am, "nachiketha Kukkady" <nachike...@in.com> wrote:
> thank you....
> but this is doing the same work which is being done by fread right????
> what i need is get some required lines like 20th, 34th,56th and so on and finally do some processings with it.... i don't want to use the loop here...
> So what can i do now????

what would be the problem to do it like this from the 'a' obtained
above?
ix = [23 34 45 56]
a = a(ix,:);

file pointer does not give the line number it is in, at least as far
as I know. So, if you want to do this, you must either have full and
absolute information about the format of your file data and probably
use it to calculate the number of byes on each line and then add that
much to your file pointer using feek or have some sort of header
information for the lines you require, to identify them. its a tedious
job and probably slower if your file is too big.

best, arun.

Subject: read

From: nachiketha Kukkady

Date: 17 Aug, 2009 03:30:18

Message: 8 of 9

hi...
i also found one more command called 'importdata'.... can i use this to read a particular line???
 

Subject: read

From: dpb

Date: 17 Aug, 2009 13:51:47

Message: 9 of 9

nachiketha Kukkady wrote:
...
> what i need is get some required lines like 20th, 34th,56th and so on
> and finally do some processings with it.... i don't want to use the
> loop here...
> So what can i do now????


 >> x=rand(4); % some sample data
x =
     0.9501 0.8913 0.8214 0.9218
     0.2311 0.7621 0.4447 0.7382
     0.6068 0.4565 0.6154 0.1763
     0.4860 0.0185 0.7919 0.4057
 >> idx=[1,3]
idx =
      1 3
 >> y = x(idx,:)
y =
     0.9501 0.8913 0.8214 0.9218
     0.6068 0.4565 0.6154 0.1763
 >> notx=[2 4]
notx =
      2 4
 >> x(notx,:) = []
x =
     0.9501 0.8913 0.8214 0.9218
     0.6068 0.4565 0.6154 0.1763
 >>

Read up on colon operator and logical indexing...

--

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
read individual... Sprinceana 17 Aug, 2009 02:08:17
read Sprinceana 17 Aug, 2009 02:08:17
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com