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: Strread String and floating point number problem! - comp.soft-sys ...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 a... Floating point problem (again) - comp.lang.asm.x86Strread String and floating point number problem! - comp.soft-sys ... Floating point problem (again) - comp.lang.asm.x86 > 10: puts("yes"); > push offset string "yes ... compare alphanumeric number with integer problem - comp.lang ...Strread String and floating point number problem! - comp.soft-sys ... Convert from scientific notation to integer - comp.unix.shell ... Strread String and floating point ... Floating Point and printf() - comp.lang.asm.x86... to integer - comp.unix.shell ... > You can avoid awk, use printf(1): % printf "%.4f\n" "3.1415926E+03" 3141.5926 ... Strread String and floating point number problem ... How do i get string representation of a number - comp.lang.java ...How can i get the first letter of this string - comp.lang.ruby ... Strread String and floating point number problem! - comp.soft-sys ... Hi John, I ... Large numbers, floating point number questions - comp.lang.c ...Strread String and floating point number problem! - comp.soft-sys ..... Post Question | Groups ... out the decimal point ... determine whether a text string is a number ... Matrix that contains both numbers and strings? - comp.soft-sys ...Strread String and floating point number problem! - comp.soft-sys ..... first column of the cell contains only the index number of the associated number, whereas I need ... int32 to double - comp.soft-sys.matlabThis should be no problem with the Floating Point Operator from Xilinx. ... Int32) (System) Rounds a double-precision floating-point value to a specified number of ... Itanium performance problem - comp.sys.hp.hpuxStrread String and floating point number problem! - comp.soft-sys ... Floating point problem (again) - comp.lang.asm.x86 > 10: puts("yes"); > push offset string "yes ... counting the digits in a number, exponential format problem - comp ...... by looping (eg via %) through the digits, d, in the number ... specific substring - comp.lang.awk Counting number of ... Strread String and floating point number problem ... Strread String and floating point number problem! - comp.soft-sys ...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 a... Floating point problem (again) - comp.lang.asm.x86 | Computer GroupStrread String and floating point number problem! - comp.soft-sys ... Floating point problem (again) - comp.lang.asm.x86 > 10: puts("yes"); > push offset string "yes ... 7/23/2012 12:30:06 AM
|