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:
convert str to number with leading zeros

Subject: convert str to number with leading zeros

From: Rajesh

Date: 12 Aug, 2010 12:06:06

Message: 1 of 13

Hi,

How can i convert the following string into the same number

'001'(string) converted to 001 (number)

str2num or str2double eats the leading zeros. any suggestions??

thanks.

Subject: convert str to number with leading zeros

From: Andy

Date: 12 Aug, 2010 12:24:05

Message: 2 of 13

"Rajesh " <rv_acharya@rediffmail.com> wrote in message <i40o3d$s48$1@fred.mathworks.com>...
> Hi,
>
> How can i convert the following string into the same number
>
> '001'(string) converted to 001 (number)
>
> str2num or str2double eats the leading zeros. any suggestions??
>
> thanks.

What? Numbers don't have leading zeros. Formatted strings representing numbers can have leading zeros. Keep your numbers as numbers until you need to format them for output. Then use sprintf to add your leading zeros back in if you must.

Subject: convert str to number with leading zeros

From: Walter Roberson

Date: 12 Aug, 2010 16:51:14

Message: 3 of 13

Rajesh wrote:

> How can i convert the following string into the same number
>
> '001'(string) converted to 001 (number)
>
> str2num or str2double eats the leading zeros. any suggestions??

Is 0/00 different than 00/0 ?

Subject: convert str to number with leading zeros

From: Walter Roberson

Date: 12 Aug, 2010 20:18:54

Message: 4 of 13

Andy wrote:
> "Rajesh " <rv_acharya@rediffmail.com> wrote in message
> <i40o3d$s48$1@fred.mathworks.com>...

>> How can i convert the following string into the same number
>>
>> '001'(string) converted to 001 (number)
>>
>> str2num or str2double eats the leading zeros. any suggestions??

> What? Numbers don't have leading zeros. Formatted strings representing
> numbers can have leading zeros.

'0' is a _numeral_ . _numbers_ are mathematical quantities independent of
numerals that may be used to denote them. Numbers do not have leading numerals
or trailing numerals or _any_ numerals: they just *are*.


Let's see if I remember my Incomplete Enchanter correctly:

"A number is a class of all classes that are similar to themselves."

Hmmm, doesn't quite look right, but I don't have my reference handy.

Subject: convert str to number with leading zeros

From: Matt Fig

Date: 12 Aug, 2010 20:30:27

Message: 5 of 13

Walter Roberson <roberson@hushmail.com> wrote in message
> Let's see if I remember my Incomplete Enchanter correctly:
>
> "A number is a class of all classes that are similar to themselves."
>
> Hmmm, doesn't quite look right, but I don't have my reference handy.


How about:

Numbers are necessary objects. By necessary we mean that the question, "Why do numbers exist?" cannot sensibly be asked.
Kinda like: Why are vacuums empty?

Subject: convert str to number with leading zeros

From: Jan Simon

Date: 12 Aug, 2010 22:31:05

Message: 6 of 13

Dear Matt,

> Kinda like: Why are vacuums empty?
Vacuums are not empty. And for quantum-mechanical reasons the plural of "vacuum" is not well defined.

Numbers are an assault on the indepency:
Together with the numbers, the obligations appear.

Jan

Subject: convert str to number with leading zeros

From: Rajesh

Date: 13 Aug, 2010 05:27:05

Message: 7 of 13

Thanks andy.

Let me elaborate my problem. I have files named image001 to image180. I want to read them one by one in a program automatically. Only the directory will be available to me. The catch here is that the names can be in different forms like say file1 to file180 or name01 to name0180 etc.

therefore i have picked up the filename of the first file and using regexp i have separated the number from the name. like in above case, say a='image' and b='001'. After this, I thought, if i get b as a number, say, c=001 i can start the loop for c=001:180, and then convert this number back to string and concat with the name part (variable a above) and read the corresponding files. but this, i guess, is not to be.

any other way of doing this.
Thanks.

Subject: convert str to number with leading zeros

From: Rajesh

Date: 13 Aug, 2010 06:52:05

Message: 8 of 13

I have also tried the following:

lets say directory= the path of directory where the files are located.

images=dir(fullfile(directory,'*.tif'));
[names index]=sortrows({images.name}');

after sorting the names in the images structure i get the following sequence

image1, image10, image100, image101, image102...image109, image11, image111, image112...image119, image12, image121.... etc
(I have 180 images in the directory. )

If i do

[names index]=sortrows({images.date}');

still doesnt give desired sequence. This time i get
image1,image10,image11,image12,...image19,image2,image20,...image29,image3,image30...etc.

this may be because i have copied this directory from its original location to my PC and now when i sort it using the 'date' field, it is taking the date of modification and not that of creation. thus when the date (and time) is the same it further sorts by name by default.

guess i am clear now. So any other ideas here.

Subject: convert str to number with leading zeros

From: Malcolm McLean

Date: 13 Aug, 2010 06:57:03

Message: 9 of 13

"Rajesh " <rv_acharya@rediffmail.com> wrote in message
> How can i convert the following string into the same number
>
> '001'(string) converted to 001 (number)
>
> str2num or str2double eats the leading zeros. any suggestions??
>
You almost certainly want to work with the number in Matlab as a binary floating point number. It's impossible in this representation to include leading zeros.

When you display it to the user, you want to represent it in Arabic denary numerals, with leading zeros.

sprintf('%03d', 1);

will achieve this (presuming ypu want to pad out to three decimal places);

Subject: convert str to number with leading zeros

From: Rajesh

Date: 13 Aug, 2010 12:05:10

Message: 10 of 13


> sprintf('%03d', 1);
>
> will achieve this (presuming ypu want to pad out to three decimal places);

Thanks MM, what if i do not know how many zeros are to be padded with. i.e. I wouldnt know if its %03d beforehand. however it will be available to me as a variable. Can i use this variable in this way.

e.g.
sprintf('% var d',1);
var can be defined just before this statement.

Subject: convert str to number with leading zeros

From: Andy

Date: 13 Aug, 2010 12:28:09

Message: 11 of 13

"Rajesh " <rv_acharya@rediffmail.com> wrote in message <i43cdm$52c$1@fred.mathworks.com>...
>
> > sprintf('%03d', 1);
> >
> > will achieve this (presuming ypu want to pad out to three decimal places);
>
> Thanks MM, what if i do not know how many zeros are to be padded with. i.e. I wouldnt know if its %03d beforehand. however it will be available to me as a variable. Can i use this variable in this way.
>
> e.g.
> sprintf('% var d',1);
> var can be defined just before this statement.

You can build up your format string for sprintf by concatenating strings with brackets:

var = 5; % var is a number representing total number of digits
sprintf(['%0' str2double(var) 'd'],1);

Subject: convert str to number with leading zeros

From: Walter Roberson

Date: 13 Aug, 2010 14:28:46

Message: 12 of 13

Rajesh wrote:
>
>> sprintf('%03d', 1);
>> will achieve this (presuming ypu want to pad out to three decimal
>> places);
>
> Thanks MM, what if i do not know how many zeros are to be padded with.
> i.e. I wouldnt know if its %03d beforehand. however it will be available
> to me as a variable. Can i use this variable in this way.
>
> e.g. sprintf('% var d',1);
> var can be defined just before this statement.

Hint:

 >> sprintf('%0*d',5,13)
ans =
00013

Subject: convert str to number with leading zeros

From: Rajesh

Date: 16 Aug, 2010 04:59:05

Message: 13 of 13

thanks folks.

Tags for 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