reading a .csv file with a space after negative sign

  • Follow


Is there any way to read a .csv file with the contents

-     1, 2

... that is a negative sign followed by some spaces and then a number?

I've tried 
fid=fopen(dir)
data = textscan(fid,'%d','Delimiter',',','whitespace','')
fclose(fid)

They give me an empty array:
fid =

     3


data = 

    {[]}


ans =

     0

Thanks~
0
Reply Judy 8/4/2010 9:51:04 PM

On Aug 4, 11:51=A0pm, "Judy " <sauwen...@gmail.com> wrote:
> Is there any way to read a .csv file with the contents
>
> - =A0 =A0 1, 2
>
> .. that is a negative sign followed by some spaces and then a number?
>
> I've tried
> fid=3Dfopen(dir)
> data =3D textscan(fid,'%d','Delimiter',',','whitespace','')
> fclose(fid)
>
> They give me an empty array:

a hint:

     data=3Dtextscan(fid,'%d','delimiter',',','whitespace','-');

us
0
Reply us 8/4/2010 9:58:45 PM


us <us@neurol.unizh.ch> wrote in message <240b660f-de2e-4a79-89dc-5da0cca75408@o19g2000yqb.googlegroups.com>...
> On Aug 4, 11:51 pm, "Judy " <sauwen...@gmail.com> wrote:
> > Is there any way to read a .csv file with the contents
> >
> > -     1, 2
> >
> > .. that is a negative sign followed by some spaces and then a number?
> >
> > I've tried
> > fid=fopen(dir)
> > data = textscan(fid,'%d','Delimiter',',','whitespace','')
> > fclose(fid)
> >
> > They give me an empty array:
> 
> a hint:
> 
>      data=textscan(fid,'%d','delimiter',',','whitespace','-');
> 
> us

Hi us,

I tried your hint. Does that insert a negative sign for every whitespace? It made the code happier, but it threw away the negative sign now so when I did
fid=fopen(dir)
 data=textscan(fid,'%d','delimiter',',','whitespace','-')
fclose(fid)
data=cell2mat(data)

data =

           1
           2

Thanks
0
Reply Judy 8/4/2010 10:18:05 PM

"Judy "
> I tried your hint. Does that insert a negative sign for every whitespace? It made the code happier, but it threw away the negative sign now so when I did
> fid=fopen(dir)
>  data=textscan(fid,'%d','delimiter',',','whitespace','-')
> fclose(fid)
> data=cell2mat(data)

ok... the assumption was that the ...negative sign... was more of a dash/flag and NOT part of the number...
in this case, things are less trivial...
you should better explain your input, eg,
- is there always a sign, including a +
- are numbers always integers
- is there always a comma between numbers
- are there always two numbers

a few(!) more typical lines of your input and what you expect the output to look like would be helpful...

us
0
Reply us 8/4/2010 10:53:04 PM

us wrote:
> "Judy "
>> I tried your hint. Does that insert a negative sign for every 
>> whitespace? It made the code happier, but it threw away the negative 
>> sign now so when I did
>> fid=fopen(dir)
>>  data=textscan(fid,'%d','delimiter',',','whitespace','-')
>> fclose(fid)
>> data=cell2mat(data)
> 
> ok... the assumption was that the ...negative sign... was more of a 
> dash/flag and NOT part of the number...
> in this case, things are less trivial...
> you should better explain your input, eg,
> - is there always a sign, including a +
> - are numbers always integers
> - is there always a comma between numbers
> - are there always two numbers

To which I would add the question of whether there is ever a negative sign for 
the second element; if there is one, does it follow immediately after the 
comma or is there a space after the comma and before the negative sign?

textscan(fid, '%c%d%d', 'delimiter', ','. 'Collect', 1)

handles the case of there never being a negative sign for the second number.
The returned cell array would have a character component that was '-' for the 
negative sign and space if there is no leading negative sign. Take the N x 2 
numeric array that is output, and negate the elements of the first column that 
correspond to the rows for which the character component is '-' .
0
Reply Walter 8/4/2010 11:01:13 PM

4 Replies
293 Views

(page loaded in 0.039 seconds)

Similiar Articles:













7/24/2012 9:47:55 AM


Reply: