Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
limit of str2double

Subject: limit of str2double

From: iffi

Date: 27 Dec, 2012 07:15:09

Message: 1 of 7

 I used this code to read a csv file of medical coded data.

f = fopen('input.dat');
 A=[];
 while ~feof(f)
    l = fgetl(f);
    r = regexp(l, ',', 'split');
    A(end+1,:) = str2double(r);
 end
 fclose (f);
 A
this code read the file well but I have also some data in this form e.g 234, 2344, V567,V1528,.. within one row
here this code also give me NaN for all 'V' CODED values apart from missing values.
str2double is not handling such codes.

Subject: limit of str2double

From: Bruno Luong

Date: 27 Dec, 2012 07:57:07

Message: 2 of 7

"iffi" wrote in message <kbgsht$aev$1@newscl01ah.mathworks.com>...
> I used this code to read a csv file of medical coded data.
>
> f = fopen('input.dat');
> A=[];
> while ~feof(f)
> l = fgetl(f);
> r = regexp(l, ',', 'split');
> A(end+1,:) = str2double(r);
> end
> fclose (f);
> A
> this code read the file well but I have also some data in this form e.g 234, 2344, V567,V1528,.. within one row
> here this code also give me NaN for all 'V' CODED values apart from missing values.
> str2double is not handling such codes.

Why not remove 'V' before calling regexp,

l = getl(f);
l(l=='V') = [];
r = ...

% Bruno

Subject: limit of str2double

From: iffi

Date: 27 Dec, 2012 08:58:07

Message: 3 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kbgv0j$i36$1@newscl01ah.mathworks.com>...
> "iffi" wrote in message <kbgsht$aev$1@newscl01ah.mathworks.com>...
> > I used this code to read a csv file of medical coded data.
> >
> > f = fopen('input.dat');
> > A=[];
> > while ~feof(f)
> > l = fgetl(f);
> > r = regexp(l, ',', 'split');
> > A(end+1,:) = str2double(r);
> > end
> > fclose (f);
> > A
> > this code read the file well but I have also some data in this form e.g 234, 2344, V567,V1528,.. within one row
> > here this code also give me NaN for all 'V' CODED values apart from missing values.
> > str2double is not handling such codes.
>
> Why not remove 'V' before calling regexp,
>
> l = getl(f);
> l(l=='V') = [];
> r = ...
>
> % Bruno

we can't remove V as it will loss the data semantics.
help me the other way around

Subject: limit of str2double

From: Ananthi

Date: 27 Dec, 2012 09:15:09

Message: 4 of 7

"iffi" wrote in message <kbh2iv$t57$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kbgv0j$i36$1@newscl01ah.mathworks.com>...
> > "iffi" wrote in message <kbgsht$aev$1@newscl01ah.mathworks.com>...
> > > I used this code to read a csv file of medical coded data.
> > >
> > > f = fopen('input.dat');
> > > A=[];
> > > while ~feof(f)
> > > l = fgetl(f);
> > > r = regexp(l, ',', 'split');
> > > A(end+1,:) = str2double(r);
> > > end
> > > fclose (f);
> > > A
> > > this code read the file well but I have also some data in this form e.g 234, 2344, V567,V1528,.. within one row
> > > here this code also give me NaN for all 'V' CODED values apart from missing values.
> > > str2double is not handling such codes.
> >
> > Why not remove 'V' before calling regexp,
> >
> > l = getl(f);
> > l(l=='V') = [];
> > r = ...
> >
> > % Bruno
>
> we can't remove V as it will loss the data semantics.
> help me the other way around

If you want to retain 'V' then use str2mat function. str2double doesnot convert the string values, hence you are getting NaN.
If str2mat is used, then you will get the numeric output, if the input is completely numeric as 234 and you will get string output if the input is a combination of 'V' and numbers. Since you have 'V' letter in your input, the output cannot be numeric.

Subject: limit of str2double

From: iffi

Date: 27 Dec, 2012 09:59:07

Message: 5 of 7

"Ananthi" wrote in message <kbh3it$32c$1@newscl01ah.mathworks.com>...
> "iffi" wrote in message <kbh2iv$t57$1@newscl01ah.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kbgv0j$i36$1@newscl01ah.mathworks.com>...
> > > "iffi" wrote in message <kbgsht$aev$1@newscl01ah.mathworks.com>...
> > > > I used this code to read a csv file of medical coded data.
> > > >
> > > > f = fopen('input.dat');
> > > > A=[];
> > > > while ~feof(f)
> > > > l = fgetl(f);
> > > > r = regexp(l, ',', 'split');
> > > > A(end+1,:) = str2double(r);
> > > > end
> > > > fclose (f);
> > > > A
> > > > this code read the file well but I have also some data in this form e.g 234, 2344, V567,V1528,.. within one row
> > > > here this code also give me NaN for all 'V' CODED values apart from missing values.
> > > > str2double is not handling such codes.
> > >
> > > Why not remove 'V' before calling regexp,
> > >
> > > l = getl(f);
> > > l(l=='V') = [];
> > > r = ...
> > >
> > > % Bruno
> >
> > we can't remove V as it will loss the data semantics.
> > help me the other way around
>
> If you want to retain 'V' then use str2mat function. str2double doesnot convert the string values, hence you are getting NaN.
> If str2mat is used, then you will get the numeric output, if the input is completely numeric as 234 and you will get string output if the input is a combination of 'V' and numbers. Since you have 'V' letter in your input, the output cannot be numeric.

thanks for your time.
str2mat function gives this error
"Subscripted assignment dimension mismatch"
I think the numbers in data set are in different hierarchy as 123, 2345, 12345. these are the varying sizes of data in all rows and columns.

Subject: limit of str2double

From: Ananthi

Date: 28 Dec, 2012 07:36:07

Message: 6 of 7

"iffi" wrote in message <kbh65b$b42$1@newscl01ah.mathworks.com>...
> "Ananthi" wrote in message <kbh3it$32c$1@newscl01ah.mathworks.com>...
> > "iffi" wrote in message <kbh2iv$t57$1@newscl01ah.mathworks.com>...
> > > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kbgv0j$i36$1@newscl01ah.mathworks.com>...
> > > > "iffi" wrote in message <kbgsht$aev$1@newscl01ah.mathworks.com>...
> > > > > I used this code to read a csv file of medical coded data.
> > > > >
> > > > > f = fopen('input.dat');
> > > > > A=[];
> > > > > while ~feof(f)
> > > > > l = fgetl(f);
> > > > > r = regexp(l, ',', 'split');
> > > > > A(end+1,:) = str2double(r);
> > > > > end
> > > > > fclose (f);
> > > > > A
> > > > > this code read the file well but I have also some data in this form e.g 234, 2344, V567,V1528,.. within one row
> > > > > here this code also give me NaN for all 'V' CODED values apart from missing values.
> > > > > str2double is not handling such codes.
> > > >
> > > > Why not remove 'V' before calling regexp,
> > > >
> > > > l = getl(f);
> > > > l(l=='V') = [];
> > > > r = ...
> > > >
> > > > % Bruno
> > >
> > > we can't remove V as it will loss the data semantics.
> > > help me the other way around
> >
> > If you want to retain 'V' then use str2mat function. str2double doesnot convert the string values, hence you are getting NaN.
> > If str2mat is used, then you will get the numeric output, if the input is completely numeric as 234 and you will get string output if the input is a combination of 'V' and numbers. Since you have 'V' letter in your input, the output cannot be numeric.
>
> thanks for your time.
> str2mat function gives this error
> "Subscripted assignment dimension mismatch"
> I think the numbers in data set are in different hierarchy as 123, 2345, 12345. these are the varying sizes of data in all rows and columns.

From your code, I understood that 'r' takes only a single input string at a time to process with str2double. I am not sure if my understanding is correct.
Can you mention how you have given your inputs? I would like to see the data structure, since am not getting any error.

Subject: limit of str2double

From: Steven_Lord

Date: 31 Dec, 2012 04:34:56

Message: 7 of 7



"iffi " <iftikhar.saman@gmail.com> wrote in message
news:kbh2iv$t57$1@newscl01ah.mathworks.com...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> <kbgv0j$i36$1@newscl01ah.mathworks.com>...
>> "iffi" wrote in message <kbgsht$aev$1@newscl01ah.mathworks.com>...
>> > I used this code to read a csv file of medical coded data.
>> >
>> > f = fopen('input.dat');
>> > A=[];
>> > while ~feof(f)
>> > l = fgetl(f);
>> > r = regexp(l, ',', 'split');
>> > A(end+1,:) = str2double(r);
>> > end
>> > fclose (f);
>> > A this code read the file well but I have also some data in this form
>> > e.g 234, 2344, V567,V1528,.. within one row
>> > here this code also give me NaN for all 'V' CODED values apart from
>> > missing values.
>> > str2double is not handling such codes.
>>
>> Why not remove 'V' before calling regexp, l = getl(f);
>> l(l=='V') = [];
>> r = ...
>>
>> % Bruno
>
> we can't remove V as it will loss the data semantics.
> help me the other way around

STR2DOUBLE is behaving correctly.

http://www.mathworks.com/help/matlab/ref/str2double.html

"If str does not represent a valid scalar value, str2double returns NaN."

'V567' is not a valid representation of a scalar value so STR2DOUBLE will
(correctly) return NaN. You will need to remove the V from the string and
convert it using STR2DOUBLE or use a different function (SSCANF perhaps?)
that can "handle" strings in that format. Regardless of which approach you
use, you're GOING to have to store separately the number and information
about whether or not the string started with a V; double precision doesn't
have the concept of a V "flag" on a number.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

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.

Contact us