Thread Subject: Reading ascii files

Subject: Reading ascii files

From: Jeremy

Date: 22 Jun, 2009 15:17:01

Message: 1 of 17


Hi,
I want to be able to read in ascii text files into a matrix in Matlab and select particular lines rows to keep. For example every 10 rows. I used a loop for the number of files. (want to perform this action to 15 files).
My problem is:
How to only read the floating point numbers discarding the text above them.
I used fscanf to read the text file but in order for it to work i needed to put syntax for every character? to discard or keep the data. For example. %*c%*c%*c%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g .... 4000 times.The textfile looks similar to this. (The columns are not the same for the header as the numbers).

Text 3 Text 5 Text2424 Text343 ffefe3 fef e3f efe>fefe3 5
1 3434.444 343434.55 >>>>>> 68
2 54545.55 454545.555
...

I just want the numbers not the part above. But the part above contains numbers and text.

This is my code:

x = dir('link');
xx = 'link';
numfiles = size(x);
for i = 3: numfiles(1,1)
jfjf = x(i).name;
fid = fopen(strcat(xx,jfjf ),'r');
a = fscanf(fid,'%c',inf); <<<<<<<<dont know how to do this

1.Put just values into matrix.
2.decide what to do with it afterwards.

fid(close);
end

Thanks,

Jeremy

Subject: Reading ascii files

From: us

Date: 22 Jun, 2009 15:45:03

Message: 2 of 17

"Jeremy " <george.panayiotou@brunel.ac.uk> wrote in message <h1o79d$410$1@fred.mathworks.com>...
>
> Hi,
> I want to be able to read in ascii text files into a matrix in Matlab and select particular lines rows to keep. For example every 10 rows. I used a loop for the number of files. (want to perform this action to 15 files).
> My problem is:
> How to only read the floating point numbers discarding the text above them.
> I used fscanf to read the text file but in order for it to work i needed to put syntax for every character? to discard or keep the data. For example. %*c%*c%*c%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g .... 4000 times.The textfile looks similar to this. (The columns are not the same for the header as the numbers).
>
> Text 3 Text 5 Text2424 Text343 ffefe3 fef e3f efe>fefe3 5
> 1 3434.444 343434.55 >>>>>> 68
> 2 54545.55 454545.555
> ...
>
> I just want the numbers not the part above. But the part above contains numbers and text.
>
> This is my code:
>
> x = dir('link');
> xx = 'link';
> numfiles = size(x);
> for i = 3: numfiles(1,1)
> jfjf = x(i).name;
> fid = fopen(strcat(xx,jfjf ),'r');
> a = fscanf(fid,'%c',inf); <<<<<<<<dont know how to do this
>
> 1.Put just values into matrix.
> 2.decide what to do with it afterwards.
>
> fid(close);
> end
>
> Thanks,
>
> Jeremy

a hint:

     help textread;
% then, look at the headerlines option...

us

Subject: Reading ascii files

From: Rune Allnor

Date: 22 Jun, 2009 15:48:16

Message: 3 of 17

On 22 Jun, 17:17, "Jeremy " <george.panayio...@brunel.ac.uk> wrote:
> Hi,
> I want to be able to read in ascii text files into a matrix in Matlab and select particular lines rows to keep. For example every 10 rows. I used a loop for the number of files. (want to perform this action to 15 files).
> My problem is:
> How to only read the floating point numbers discarding the text above them.
> I used fscanf to read the text file but in order for it to work i needed to put syntax for every character? to discard or keep the data. For example. %*c%*c%*c%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g .... 4000 times.The textfile looks similar to this. (The columns are not the same for the header as the numbers).
>
> Text  3 Text 5 Text2424 Text343 ffefe3 fef e3f efe>fefe3 5
> 1           3434.444          343434.55             >>>>>> 68
> 2           54545.55          454545.555
> ...
>
> I just want the numbers not the part above. But the part above contains numbers and text.

Try regular expressions. Check if any one line contains
anything other than digits, commas, dots or whitespaces.
If it does, discard it. Otherwise, extract the numbers.

Rune

Subject: Reading ascii files

From: Jeremy

Date: 22 Jun, 2009 16:05:03

Message: 4 of 17

"us " <us@neurol.unizh.ch> wrote in message <h1o8tv$okl$1@fred.mathworks.com>...
> "Jeremy " <george.panayiotou@brunel.ac.uk> wrote in message <h1o79d$410$1@fred.mathworks.com>...
> >
> > Hi,
> > I want to be able to read in ascii text files into a matrix in Matlab and select particular lines rows to keep. For example every 10 rows. I used a loop for the number of files. (want to perform this action to 15 files).
> > My problem is:
> > How to only read the floating point numbers discarding the text above them.
> > I used fscanf to read the text file but in order for it to work i needed to put syntax for every character? to discard or keep the data. For example. %*c%*c%*c%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g .... 4000 times.The textfile looks similar to this. (The columns are not the same for the header as the numbers).
> >
> > Text 3 Text 5 Text2424 Text343 ffefe3 fef e3f efe>fefe3 5
> > 1 3434.444 343434.55 >>>>>> 68
> > 2 54545.55 454545.555
> > ...
> >
> > I just want the numbers not the part above. But the part above contains numbers and text.
> >
> > This is my code:
> >
> > x = dir('link');
> > xx = 'link';
> > numfiles = size(x);
> > for i = 3: numfiles(1,1)
> > jfjf = x(i).name;
> > fid = fopen(strcat(xx,jfjf ),'r');
> > a = fscanf(fid,'%c',inf); <<<<<<<<dont know how to do this
> >
> > 1.Put just values into matrix.
> > 2.decide what to do with it afterwards.
> >
> > fid(close);
> > end
> >
> > Thanks,
> >
> > Jeremy
>
> a hint:
>
> help textread;
> % then, look at the headerlines option...
>
> us

Hi,
Thanks for the reply. I did what you said us. It looks like this. Bad? lol. Worked though:

[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48,a49,a50,a51,a52,a53,a54,a55,a56,a57,a58,a59,a60,a61,a62,a63,a64,a65,a66,a67,a68] = textread(ffffff, '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s','headerlines',5);
XYZ1 = [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48,a49,a50,a51,a52,a53,a54,a55,a56,a57,a58,a59,a60,a61,a62,a63,a64,a65,a66,a67,a68];
XYZ2 = str2double(XYZ1);

It might be better to manually extract the floating point values but I need more time to think how to do this because I am new. I would use fscanf to get the data in char format and use if statments? Thanks for your help though.

Kind Regards,

Jeremy

Subject: Reading ascii files

From: us

Date: 22 Jun, 2009 16:27:01

Message: 5 of 17

"Jeremy " <george.panayiotou@brunel.ac.uk> wrote in message <h1oa3f$dc7$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <h1o8tv$okl$1@fred.mathworks.com>...
> > "Jeremy " <george.panayiotou@brunel.ac.uk> wrote in message <h1o79d$410$1@fred.mathworks.com>...
> > >
> > > Hi,
> > > I want to be able to read in ascii text files into a matrix in Matlab and select particular lines rows to keep. For example every 10 rows. I used a loop for the number of files. (want to perform this action to 15 files).
> > > My problem is:
> > > How to only read the floating point numbers discarding the text above them.
> > > I used fscanf to read the text file but in order for it to work i needed to put syntax for every character? to discard or keep the data. For example. %*c%*c%*c%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g .... 4000 times.The textfile looks similar to this. (The columns are not the same for the header as the numbers).
> > >
> > > Text 3 Text 5 Text2424 Text343 ffefe3 fef e3f efe>fefe3 5
> > > 1 3434.444 343434.55 >>>>>> 68
> > > 2 54545.55 454545.555
> > > ...
> > >
> > > I just want the numbers not the part above. But the part above contains numbers and text.
> > >
> > > This is my code:
> > >
> > > x = dir('link');
> > > xx = 'link';
> > > numfiles = size(x);
> > > for i = 3: numfiles(1,1)
> > > jfjf = x(i).name;
> > > fid = fopen(strcat(xx,jfjf ),'r');
> > > a = fscanf(fid,'%c',inf); <<<<<<<<dont know how to do this
> > >
> > > 1.Put just values into matrix.
> > > 2.decide what to do with it afterwards.
> > >
> > > fid(close);
> > > end
> > >
> > > Thanks,
> > >
> > > Jeremy
> >
> > a hint:
> >
> > help textread;
> > % then, look at the headerlines option...
> >
> > us
>
> Hi,
> Thanks for the reply. I did what you said us. It looks like this. Bad? lol. Worked though:
>
> [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48,a49,a50,a51,a52,a53,a54,a55,a56,a57,a58,a59,a60,a61,a62,a63,a64,a65,a66,a67,a68] = textread(ffffff, '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s','headerlines',5);

nah...

     clear a;
     [a{1:68}]=texread(ffffff,'%s','headerlines',5,'delimiter','','whitespace','');
% then, look at your As and convert them smartly...

us

Subject: Reading ascii files

From: Jeremy

Date: 22 Jun, 2009 16:42:02

Message: 6 of 17

"us " <us@neurol.unizh.ch> wrote in message <h1obcl$90i$1@fred.mathworks.com>...
> "Jeremy " <george.panayiotou@brunel.ac.uk> wrote in message <h1oa3f$dc7$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <h1o8tv$okl$1@fred.mathworks.com>...
> > > "Jeremy " <george.panayiotou@brunel.ac.uk> wrote in message <h1o79d$410$1@fred.mathworks.com>...
> > > >
> > > > Hi,
> > > > I want to be able to read in ascii text files into a matrix in Matlab and select particular lines rows to keep. For example every 10 rows. I used a loop for the number of files. (want to perform this action to 15 files).
> > > > My problem is:
> > > > How to only read the floating point numbers discarding the text above them.
> > > > I used fscanf to read the text file but in order for it to work i needed to put syntax for every character? to discard or keep the data. For example. %*c%*c%*c%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g .... 4000 times.The textfile looks similar to this. (The columns are not the same for the header as the numbers).
> > > >
> > > > Text 3 Text 5 Text2424 Text343 ffefe3 fef e3f efe>fefe3 5
> > > > 1 3434.444 343434.55 >>>>>> 68
> > > > 2 54545.55 454545.555
> > > > ...
> > > >
> > > > I just want the numbers not the part above. But the part above contains numbers and text.
> > > >
> > > > This is my code:
> > > >
> > > > x = dir('link');
> > > > xx = 'link';
> > > > numfiles = size(x);
> > > > for i = 3: numfiles(1,1)
> > > > jfjf = x(i).name;
> > > > fid = fopen(strcat(xx,jfjf ),'r');
> > > > a = fscanf(fid,'%c',inf); <<<<<<<<dont know how to do this
> > > >
> > > > 1.Put just values into matrix.
> > > > 2.decide what to do with it afterwards.
> > > >
> > > > fid(close);
> > > > end
> > > >
> > > > Thanks,
> > > >
> > > > Jeremy
> > >
> > > a hint:
> > >
> > > help textread;
> > > % then, look at the headerlines option...
> > >
> > > us
> >
> > Hi,
> > Thanks for the reply. I did what you said us. It looks like this. Bad? lol. Worked though:
> >
> > [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48,a49,a50,a51,a52,a53,a54,a55,a56,a57,a58,a59,a60,a61,a62,a63,a64,a65,a66,a67,a68] = textread(ffffff, '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s','headerlines',5);
>
> nah...
>
> clear a;
> [a{1:68}]=texread(ffffff,'%s','headerlines',5,'delimiter','','whitespace','');
> % then, look at your As and convert them smartly...
>
> us

Hi,

i tried to use this and I get an error that says:
"Number of outputs must match the number of unskipped input fields."
because there is only one '%s' ?
Also what does As mean? Thank you.

Jeremy

Subject: Reading ascii files

From: us

Date: 22 Jun, 2009 17:10:18

Message: 7 of 17

"Jeremy "
> > clear a;
> > [a{1:68}]=texread(ffffff,'%s','headerlines',5,'delimiter','','whitespace','');
> > % then, look at your As and convert them smartly...

> i tried to use this and I get an error that says:
> "Number of outputs must match the number of unskipped input fields."
> because there is only one '%s' ?
> Also what does As mean? Thank you.

yes, sorry for causing confusion(!)...

     clear a;
     nf=68; % <- #fields you have
     fmt=repmat('%s ',1,nf);
     [a{1:nf}]=textread(ffffff,fmt,'headerlines',5,'delimiter',' ','whitespace','');
% As simply means a{...}, which contain the values of a single column

us

Subject: Reading ascii files

From: Jeremy

Date: 22 Jun, 2009 17:35:02

Message: 8 of 17

"us " <us@neurol.unizh.ch> wrote in message <h1odtp$rj5$1@fred.mathworks.com>...
> "Jeremy "
> > > clear a;
> > > [a{1:68}]=texread(ffffff,'%s','headerlines',5,'delimiter','','whitespace','');
> > > % then, look at your As and convert them smartly...
>
> > i tried to use this and I get an error that says:
> > "Number of outputs must match the number of unskipped input fields."
> > because there is only one '%s' ?
> > Also what does As mean? Thank you.
>
> yes, sorry for causing confusion(!)...
>
> clear a;
> nf=68; % <- #fields you have
> fmt=repmat('%s ',1,nf);
> [a{1:nf}]=textread(ffffff,fmt,'headerlines',5,'delimiter',' ','whitespace','');
> % As simply means a{...}, which contain the values of a single column
>
> us

Thanks us. That works. Now what I have is a 588x1 nested cell structure and I am trying to convert this to a single floating point double matrix. cell2mat does not work because it states "Cannot support cell arrays containing cell arrays or objects." Do you know how I can convert this? I want to carry out some calculations on the data but before that i need to remove some lines manually. Using a for loop would be best I guess. Any help appreciated about conversion.

Thank you,

Jeremy

Subject: Reading ascii files

From: us

Date: 22 Jun, 2009 17:53:01

Message: 9 of 17

"Jeremy " <jeremy@mytrashmail.com> wrote in message <h1ofc6$560$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <h1odtp$rj5$1@fred.mathworks.com>...
> > "Jeremy "
> > > > clear a;
> > > > [a{1:68}]=texread(ffffff,'%s','headerlines',5,'delimiter','','whitespace','');
> > > > % then, look at your As and convert them smartly...
> >
> > > i tried to use this and I get an error that says:
> > > "Number of outputs must match the number of unskipped input fields."
> > > because there is only one '%s' ?
> > > Also what does As mean? Thank you.
> >
> > yes, sorry for causing confusion(!)...
> >
> > clear a;
> > nf=68; % <- #fields you have
> > fmt=repmat('%s ',1,nf);
> > [a{1:nf}]=textread(ffffff,fmt,'headerlines',5,'delimiter',' ','whitespace','');
> > % As simply means a{...}, which contain the values of a single column
> >
> > us
>
> Thanks us. That works. Now what I have is a 588x1 nested cell structure and I am trying to convert this to a single floating point double matrix. cell2mat does not work because it states "Cannot support cell arrays containing cell arrays or objects." Do you know how I can convert this? I want to carry out some calculations on the data but before that i need to remove some lines manually. Using a for loop would be best I guess. Any help appreciated about conversion.
>
> Thank you,
>
> Jeremy

it's a bit tough to help without having a clue of the content of each col-cell...
anyhow, as a first step you could reconstruct your file's content into a cell array CA where each col represents one col in your file...

     ca=cat(2,a{:});
% now...

us

Subject: Reading ascii files

From: Jeremy

Date: 22 Jun, 2009 18:21:01

Message: 10 of 17

"us " <us@neurol.unizh.ch> wrote in message <h1ogdt$en1$1@fred.mathworks.com>...
> "Jeremy " <jeremy@mytrashmail.com> wrote in message <h1ofc6$560$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <h1odtp$rj5$1@fred.mathworks.com>...
> > > "Jeremy "
> > > > > clear a;
> > > > > [a{1:68}]=texread(ffffff,'%s','headerlines',5,'delimiter','','whitespace','');
> > > > > % then, look at your As and convert them smartly...
> > >
> > > > i tried to use this and I get an error that says:
> > > > "Number of outputs must match the number of unskipped input fields."
> > > > because there is only one '%s' ?
> > > > Also what does As mean? Thank you.
> > >
> > > yes, sorry for causing confusion(!)...
> > >
> > > clear a;
> > > nf=68; % <- #fields you have
> > > fmt=repmat('%s ',1,nf);
> > > [a{1:nf}]=textread(ffffff,fmt,'headerlines',5,'delimiter',' ','whitespace','');
> > > % As simply means a{...}, which contain the values of a single column
> > >
> > > us
> >
> > Thanks us. That works. Now what I have is a 588x1 nested cell structure and I am trying to convert this to a single floating point double matrix. cell2mat does not work because it states "Cannot support cell arrays containing cell arrays or objects." Do you know how I can convert this? I want to carry out some calculations on the data but before that i need to remove some lines manually. Using a for loop would be best I guess. Any help appreciated about conversion.
> >
> > Thank you,
> >
> > Jeremy
>
> it's a bit tough to help without having a clue of the content of each col-cell...
> anyhow, as a first step you could reconstruct your file's content into a cell array CA where each col represents one col in your file...
>
> ca=cat(2,a{:});
> % now...
>
> us

1. The contents of the ascii file discarding the 5 line header part (when we used the textread headerline parameter) are all floating point numbers which tab delimited.

2. fmt = repmat('%s ',1,nf); Gives me a 1x204char which is correct(3*68).
    when i use a{1:nf} this creates a Cell Array 1by68 containing 588x1 in each cell. What it has done is put the entire column in one cell. (there are 588 rows in each column). I understand this.

3.when i use ca=cat(2,a{:}); I get

Columns 1 through 14

    [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
    [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''

Subject: Reading ascii files

From: us

Date: 22 Jun, 2009 18:32:01

Message: 11 of 17

"Jeremy "

> 1. The contents of the ascii file discarding the 5 line header part (when we used the textread headerline parameter) are all floating point numbers which tab delimited...

well, THAT's a different story...
- you must add the TAB as an additional delimiter, which now reads ' \t' == 'SPACE TAB'
- or, if there are no SPACEs, just use a '\t'

yet another try...

     clear a;
     nf=68; % <- #fields you have
     fmt=repmat('%s ',1,nf);
     [a{1:nf}]=textread(ffffff,fmt,'headerlines',5,'delimiter','\t','whitespace','');

us

Subject: Reading ascii files

From: Jeremy

Date: 22 Jun, 2009 18:36:01

Message: 12 of 17

"Jeremy " <jeremy@mytrashmail.com> wrote in message <h1oi2d$3fq$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <h1ogdt$en1$1@fred.mathworks.com>...
> > "Jeremy " <jeremy@mytrashmail.com> wrote in message <h1ofc6$560$1@fred.mathworks.com>...
> > > "us " <us@neurol.unizh.ch> wrote in message <h1odtp$rj5$1@fred.mathworks.com>...
> > > > "Jeremy "
> > > > > > clear a;
> > > > > > [a{1:68}]=texread(ffffff,'%s','headerlines',5,'delimiter','','whitespace','');
> > > > > > % then, look at your As and convert them smartly...
> > > >
> > > > > i tried to use this and I get an error that says:
> > > > > "Number of outputs must match the number of unskipped input fields."
> > > > > because there is only one '%s' ?
> > > > > Also what does As mean? Thank you.
> > > >
> > > > yes, sorry for causing confusion(!)...
> > > >
> > > > clear a;
> > > > nf=68; % <- #fields you have
> > > > fmt=repmat('%s ',1,nf);
> > > > [a{1:nf}]=textread(ffffff,fmt,'headerlines',5,'delimiter',' ','whitespace','');
> > > > % As simply means a{...}, which contain the values of a single column
> > > >
> > > > us
> > >
> > > Thanks us. That works. Now what I have is a 588x1 nested cell structure and I am trying to convert this to a single floating point double matrix. cell2mat does not work because it states "Cannot support cell arrays containing cell arrays or objects." Do you know how I can convert this? I want to carry out some calculations on the data but before that i need to remove some lines manually. Using a for loop would be best I guess. Any help appreciated about conversion.
> > >
> > > Thank you,
> > >
> > > Jeremy
> >
> > it's a bit tough to help without having a clue of the content of each col-cell...
> > anyhow, as a first step you could reconstruct your file's content into a cell array CA where each col represents one col in your file...
> >
> > ca=cat(2,a{:});
> > % now...
> >
> > us
>
> 1. The contents of the ascii file discarding the 5 line header part (when we used the textread headerline parameter) are all floating point numbers which tab delimited.
>
> 2. fmt = repmat('%s ',1,nf); Gives me a 1x204char which is correct(3*68).
> when i use a{1:nf} this creates a Cell Array 1by68 containing 588x1 in each cell. What it has done is put the entire column in one cell. (there are 588 rows in each column). I understand this.
>
> 3.when i use ca=cat(2,a{:}); I get
>
> Columns 1 through 14
>
> [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''

what I want to do is convert each 588x1 in the 1by68 nested cell array in double format

Subject: Reading ascii files

From: us

Date: 22 Jun, 2009 18:42:02

Message: 13 of 17

"Jeremy "
> what I want to do is convert each 588x1 in the 1by68 nested cell array in double format...

no, did you look at the TAB solution presented in the last reply...
you should be able to easily retrieve each col into one single A...

us

Subject: Reading ascii files

From: Jeremy

Date: 22 Jun, 2009 18:44:02

Message: 14 of 17

"Jeremy " <jeremy@mytrashmail.com> wrote in message <h1oiuh$2h8$1@fred.mathworks.com>...
> "Jeremy " <jeremy@mytrashmail.com> wrote in message <h1oi2d$3fq$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <h1ogdt$en1$1@fred.mathworks.com>...
> > > "Jeremy " <jeremy@mytrashmail.com> wrote in message <h1ofc6$560$1@fred.mathworks.com>...
> > > > "us " <us@neurol.unizh.ch> wrote in message <h1odtp$rj5$1@fred.mathworks.com>...
> > > > > "Jeremy "
> > > > > > > clear a;
> > > > > > > [a{1:68}]=texread(ffffff,'%s','headerlines',5,'delimiter','','whitespace','');
> > > > > > > % then, look at your As and convert them smartly...
> > > > >
> > > > > > i tried to use this and I get an error that says:
> > > > > > "Number of outputs must match the number of unskipped input fields."
> > > > > > because there is only one '%s' ?
> > > > > > Also what does As mean? Thank you.
> > > > >
> > > > > yes, sorry for causing confusion(!)...
> > > > >
> > > > > clear a;
> > > > > nf=68; % <- #fields you have
> > > > > fmt=repmat('%s ',1,nf);
> > > > > [a{1:nf}]=textread(ffffff,fmt,'headerlines',5,'delimiter',' ','whitespace','');
> > > > > % As simply means a{...}, which contain the values of a single column
> > > > >
> > > > > us
> > > >
> > > > Thanks us. That works. Now what I have is a 588x1 nested cell structure and I am trying to convert this to a single floating point double matrix. cell2mat does not work because it states "Cannot support cell arrays containing cell arrays or objects." Do you know how I can convert this? I want to carry out some calculations on the data but before that i need to remove some lines manually. Using a for loop would be best I guess. Any help appreciated about conversion.
> > > >
> > > > Thank you,
> > > >
> > > > Jeremy
> > >
> > > it's a bit tough to help without having a clue of the content of each col-cell...
> > > anyhow, as a first step you could reconstruct your file's content into a cell array CA where each col represents one col in your file...
> > >
> > > ca=cat(2,a{:});
> > > % now...
> > >
> > > us
> >
> > 1. The contents of the ascii file discarding the 5 line header part (when we used the textread headerline parameter) are all floating point numbers which tab delimited.
> >
> > 2. fmt = repmat('%s ',1,nf); Gives me a 1x204char which is correct(3*68).
> > when i use a{1:nf} this creates a Cell Array 1by68 containing 588x1 in each cell. What it has done is put the entire column in one cell. (there are 588 rows in each column). I understand this.
> >
> > 3.when i use ca=cat(2,a{:}); I get
> >
> > Columns 1 through 14
> >
> > [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x659 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
> > [1x660 char] '' '' '' '' '' '' '' '' '' '' '' '' ''
>
> what I want to do is convert each 588x1 in the 1by68 nested cell array in double format

Yes this has worked now I can see the floating point numbers in the command window. One problem is that in part of the file there is a gap inbetween the data so I believe this has read it as an empty space. Thank you very much for your help though.

Kind regards,

Jeremy

Subject: Reading ascii files

From: Jeremy

Date: 22 Jun, 2009 19:01:02

Message: 15 of 17

"us " <us@neurol.unizh.ch> wrote in message <h1oj9p$p98$1@fred.mathworks.com>...
> "Jeremy "
> > what I want to do is convert each 588x1 in the 1by68 nested cell array in double format...
>
> no, did you look at the TAB solution presented in the last reply...
> you should be able to easily retrieve each col into one single A...
>
> us

sorry about the double post i didnt see it. now what i have is a 588x68 cell array containing the numerical data. I used XYZ2 = str2double (ca) and it has converted the data to double. Im not sure if this is the quickest way though. regarding empty NAN values. I have to manually remove them? I am worried about computation time. Thanks,

Jeremy

Subject: Reading ascii files

From: us

Date: 22 Jun, 2009 19:11:01

Message: 16 of 17

"Jeremy " <jeremy@mytrashmail.com> wrote in message <h1okde$akj$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <h1oj9p$p98$1@fred.mathworks.com>...
> > "Jeremy "
> > > what I want to do is convert each 588x1 in the 1by68 nested cell array in double format...
> >
> > no, did you look at the TAB solution presented in the last reply...
> > you should be able to easily retrieve each col into one single A...
> >
> > us
>
> sorry about the double post i didnt see it. now what i have is a 588x68 cell array containing the numerical data. I used XYZ2 = str2double (ca) and it has converted the data to double. Im not sure if this is the quickest way though. regarding empty NAN values. I have to manually remove them? I am worried about computation time. Thanks,
>
> Jeremy

i'd use this approach
- STR2DOUBLE comes with a lot of overhead...

% assume ca contains your cells...

     r=cellfun(@(x) sscanf(x,'%f'),ca,'uni',false);
% note: the cell array R contains
% - floating point values if a conversion was possible
% - an empty cell '' when if was not possible
% you can easily manipulate R, eg, cell2mat, cat, and so on...

us

Subject: Reading ascii files

From: Jeremy

Date: 23 Jun, 2009 18:40:18

Message: 17 of 17

"us " <us@neurol.unizh.ch> wrote in message <h1ol05$k7e$1@fred.mathworks.com>...
> "Jeremy " <jeremy@mytrashmail.com> wrote in message <h1okde$akj$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <h1oj9p$p98$1@fred.mathworks.com>...
> > > "Jeremy "
> > > > what I want to do is convert each 588x1 in the 1by68 nested cell array in double format...
> > >
> > > no, did you look at the TAB solution presented in the last reply...
> > > you should be able to easily retrieve each col into one single A...
> > >
> > > us
> >
> > sorry about the double post i didnt see it. now what i have is a 588x68 cell array containing the numerical data. I used XYZ2 = str2double (ca) and it has converted the data to double. Im not sure if this is the quickest way though. regarding empty NAN values. I have to manually remove them? I am worried about computation time. Thanks,
> >
> > Jeremy
>
> i'd use this approach
> - STR2DOUBLE comes with a lot of overhead...
>
> % assume ca contains your cells...
>
> r=cellfun(@(x) sscanf(x,'%f'),ca,'uni',false);
> % note: the cell array R contains
> % - floating point values if a conversion was possible
> % - an empty cell '' when if was not possible
> % you can easily manipulate R, eg, cell2mat, cat, and so on...
>
> us

Hi,

Thanks us thats great. So I have my floating point numerical data inside cells. Do I need to use cellfun in order to perform a calculation such as subtract one value in one cell by another? I get an error saying "Undefined function or method 'minus' for input arguments of type 'cell'." I assume it is less computational expensive to work in the cell format rather than double. After I calculate the data I want to export it to a text file.

Thanks,

Jeremy

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
sscanf us 22 Jun, 2009 15:14:05
cat us 22 Jun, 2009 13:54:08
reference us 22 Jun, 2009 11:49:06
ascii Sprinceana 22 Jun, 2009 11:43:20
ascii files Sprinceana 22 Jun, 2009 11:43:20
rssFeed for this Thread

Contact us at files@mathworks.com