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.
Is 0/00 different than 00/0 ?
>> 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.
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?
> 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
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.
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.
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);
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);
Hint:
>> sprintf('%0*d',5,13)
ans =
00013