Strread String and floating point number problem!

  • Follow


Hi,

I am trying to use strread, to read two columns of data in a text file. The columns are like this:

B00001  6.125105
B00002  6.714116

I am trying to get the data into two separate vector fields. My code is as follows, where 'Rows' is extracted from the file and is the number of rows of data in the format above:

% Open the text file:
fid = fopen ( filename, 'r' );

% Pre-allocate memory for the two columns of data:
alpha_file = zeros ( Rows,1 );
alpha = zeros ( Rows,1 );

% Read each line of data in the file:
for i = 1:Rows
    line = fgetl ( fid );
    [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
end


I have also tried replacing [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
with [alpha_file(i), alpha(i)] = strread( line, '%f %f' ); and it doesn't like it either. Any suggestions on how to get this working are greatly welcomed.

Many Thanks,

Robbie
0
Reply Robbie 11/24/2010 10:07:05 AM

"Robbie " <rstevens01@qub.ac.uk> wrote in message <icio49$99g$1@fred.mathworks.com>...
> Hi,
> 
> I am trying to use strread, to read two columns of data in a text file. The columns are like this:
> 
> B00001  6.125105
> B00002  6.714116
> 
> I am trying to get the data into two separate vector fields. My code is as follows, where 'Rows' is extracted from the file and is the number of rows of data in the format above:
> 
> % Open the text file:
> fid = fopen ( filename, 'r' );
> 
> % Pre-allocate memory for the two columns of data:
> alpha_file = zeros ( Rows,1 );
> alpha = zeros ( Rows,1 );
> 
> % Read each line of data in the file:
> for i = 1:Rows
>     line = fgetl ( fid );
>     [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
> end
> 
> 
> I have also tried replacing [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
> with [alpha_file(i), alpha(i)] = strread( line, '%f %f' ); and it doesn't like it either. Any suggestions on how to get this working are greatly welcomed.
> 
> Many Thanks,
> 
> Robbie

Hi Robbie,

What about a simple one line code after getting fid:
yourCells = textscan(fid, '%*c %d %f %*[^\n]', 'delimiter', ',');

As output, you will have a cell array. Then you can use it or convert it to a matrix.

Regards.
0
Reply John 11/24/2010 10:26:06 AM


"John " <onsekizdegilim@yahoo.co.uk> wrote in message <icip7u$kod$1@fred.mathworks.com>...
> "Robbie " <rstevens01@qub.ac.uk> wrote in message <icio49$99g$1@fred.mathworks.com>...
> > Hi,
> > 
> > I am trying to use strread, to read two columns of data in a text file. The columns are like this:
> > 
> > B00001  6.125105
> > B00002  6.714116
> > 
> > I am trying to get the data into two separate vector fields. My code is as follows, where 'Rows' is extracted from the file and is the number of rows of data in the format above:
> > 
> > % Open the text file:
> > fid = fopen ( filename, 'r' );
> > 
> > % Pre-allocate memory for the two columns of data:
> > alpha_file = zeros ( Rows,1 );
> > alpha = zeros ( Rows,1 );
> > 
> > % Read each line of data in the file:
> > for i = 1:Rows
> >     line = fgetl ( fid );
> >     [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
> > end
> > 
> > 
> > I have also tried replacing [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
> > with [alpha_file(i), alpha(i)] = strread( line, '%f %f' ); and it doesn't like it either. Any suggestions on how to get this working are greatly welcomed.
> > 
> > Many Thanks,
> > 
> > Robbie
> 
> Hi Robbie,
> 
> What about a simple one line code after getting fid:
> yourCells = textscan(fid, '%*c %d %f %*[^\n]', 'delimiter', ',');
> 
> As output, you will have a cell array. Then you can use it or convert it to a matrix.
> 
> Regards.

please ignore the delimiter since you don't need it.
yourCells = textscan(fid, '%*c %d %f %*[^\n]');
0
Reply John 11/24/2010 10:36:05 AM

"John " <onsekizdegilim@yahoo.co.uk> wrote in message <icip7u$kod$1@fred.mathworks.com>...
> "Robbie " <rstevens01@qub.ac.uk> wrote in message <icio49$99g$1@fred.mathworks.com>...
> > Hi,
> > 
> > I am trying to use strread, to read two columns of data in a text file. The columns are like this:
> > 
> > B00001  6.125105
> > B00002  6.714116
> > 
> > I am trying to get the data into two separate vector fields. My code is as follows, where 'Rows' is extracted from the file and is the number of rows of data in the format above:
> > 
> > % Open the text file:
> > fid = fopen ( filename, 'r' );
> > 
> > % Pre-allocate memory for the two columns of data:
> > alpha_file = zeros ( Rows,1 );
> > alpha = zeros ( Rows,1 );
> > 
> > % Read each line of data in the file:
> > for i = 1:Rows
> >     line = fgetl ( fid );
> >     [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
> > end
> > 
> > 
> > I have also tried replacing [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
> > with [alpha_file(i), alpha(i)] = strread( line, '%f %f' ); and it doesn't like it either. Any suggestions on how to get this working are greatly welcomed.
> > 
> > Many Thanks,
> > 
> > Robbie
> 
> Hi Robbie,
> 
> What about a simple one line code after getting fid:
> yourCells = textscan(fid, '%*c %d %f %*[^\n]', 'delimiter', ',');
> 
> As output, you will have a cell array. Then you can use it or convert it to a matrix.
> 
> Regards.

Hi John,

I can seem to only get this to partially work, the first column of the cell contains only the index number of the associated number, whereas I need the full string i.e. B00001.

Many Thanks,

Robbie
0
Reply Robbie 11/24/2010 10:46:07 AM

"Robbie " <rstevens01@qub.ac.uk> wrote in message <iciqdf$68h$1@fred.mathworks.com>...
> "John " <onsekizdegilim@yahoo.co.uk> wrote in message <icip7u$kod$1@fred.mathworks.com>...
> > "Robbie " <rstevens01@qub.ac.uk> wrote in message <icio49$99g$1@fred.mathworks.com>...
> > > Hi,
> > > 
> > > I am trying to use strread, to read two columns of data in a text file. The columns are like this:
> > > 
> > > B00001  6.125105
> > > B00002  6.714116
> > > 
> > > I am trying to get the data into two separate vector fields. My code is as follows, where 'Rows' is extracted from the file and is the number of rows of data in the format above:
> > > 
> > > % Open the text file:
> > > fid = fopen ( filename, 'r' );
> > > 
> > > % Pre-allocate memory for the two columns of data:
> > > alpha_file = zeros ( Rows,1 );
> > > alpha = zeros ( Rows,1 );
> > > 
> > > % Read each line of data in the file:
> > > for i = 1:Rows
> > >     line = fgetl ( fid );
> > >     [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
> > > end
> > > 
> > > 
> > > I have also tried replacing [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
> > > with [alpha_file(i), alpha(i)] = strread( line, '%f %f' ); and it doesn't like it either. Any suggestions on how to get this working are greatly welcomed.
> > > 
> > > Many Thanks,
> > > 
> > > Robbie
> > 
> > Hi Robbie,
> > 
> > What about a simple one line code after getting fid:
> > yourCells = textscan(fid, '%*c %d %f %*[^\n]', 'delimiter', ',');
> > 
> > As output, you will have a cell array. Then you can use it or convert it to a matrix.
> > 
> > Regards.
> 
> Hi John,
> 
> I can seem to only get this to partially work, the first column of the cell contains only the index number of the associated number, whereas I need the full string i.e. B00001.
> 
> Many Thanks,
> 
> Robbie

Then, first

help textscan

and 

yourCells = textscan(fid, '%s %f %*[^\n]');
0
Reply John 11/24/2010 10:58:06 AM

"John " <onsekizdegilim@yahoo.co.uk> wrote in message <icir3u$k4p$1@fred.mathworks.com>...
> "Robbie " <rstevens01@qub.ac.uk> wrote in message <iciqdf$68h$1@fred.mathworks.com>...
> > "John " <onsekizdegilim@yahoo.co.uk> wrote in message <icip7u$kod$1@fred.mathworks.com>...
> > > "Robbie " <rstevens01@qub.ac.uk> wrote in message <icio49$99g$1@fred.mathworks.com>...
> > > > Hi,
> > > > 
> > > > I am trying to use strread, to read two columns of data in a text file. The columns are like this:
> > > > 
> > > > B00001  6.125105
> > > > B00002  6.714116
> > > > 
> > > > I am trying to get the data into two separate vector fields. My code is as follows, where 'Rows' is extracted from the file and is the number of rows of data in the format above:
> > > > 
> > > > % Open the text file:
> > > > fid = fopen ( filename, 'r' );
> > > > 
> > > > % Pre-allocate memory for the two columns of data:
> > > > alpha_file = zeros ( Rows,1 );
> > > > alpha = zeros ( Rows,1 );
> > > > 
> > > > % Read each line of data in the file:
> > > > for i = 1:Rows
> > > >     line = fgetl ( fid );
> > > >     [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
> > > > end
> > > > 
> > > > 
> > > > I have also tried replacing [alpha_file(i), alpha(i)] = strread( line, '%s %f' );
> > > > with [alpha_file(i), alpha(i)] = strread( line, '%f %f' ); and it doesn't like it either. Any suggestions on how to get this working are greatly welcomed.
> > > > 
> > > > Many Thanks,
> > > > 
> > > > Robbie
> > > 
> > > Hi Robbie,
> > > 
> > > What about a simple one line code after getting fid:
> > > yourCells = textscan(fid, '%*c %d %f %*[^\n]', 'delimiter', ',');
> > > 
> > > As output, you will have a cell array. Then you can use it or convert it to a matrix.
> > > 
> > > Regards.
> > 
> > Hi John,
> > 
> > I can seem to only get this to partially work, the first column of the cell contains only the index number of the associated number, whereas I need the full string i.e. B00001.
> > 
> > Many Thanks,
> > 
> > Robbie
> 
> Then, first
> 
> help textscan
> 
> and 
> 
> yourCells = textscan(fid, '%s %f %*[^\n]');

Thanks John, I managed to get it working, your help is much appreciated.

Best Regards,

Robbie
0
Reply Robbie 11/24/2010 12:08:04 PM

5 Replies
275 Views

(page loaded in 0.026 seconds)

Similiar Articles:













7/23/2012 12:30:06 AM


Reply: