MATLAB: Str2double/str2num

str2doublestr2num

day='0001'; str2double(day) would give me 1 as answer, how do I get 0001 exact four number of digits for instance?

Best Answer

Floating point variables do not have leading 0's physically stored in memory (not counting the denormalized numbers of course). So 0001 and 1 are stored exactly the same in memory. If you want to display the leading 0's then you need to use a format that specifies that on print out. E.g.,
>> day = '0001'
day =
0001
>> d = str2double(day)
d =
1
>> fprintf('%04d\n',d)
0001