How can I to convert a cell to a double without losing the leading zeros?
33 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
pink flower
am 20 Sep. 2020
Kommentiert: Star Strider
am 20 Sep. 2020
I have a loop where I am trying to extract the numbers and convert them to double. However, when the numbers are "0000", the result is just "0". How can I do this?
for x = 1:length(ix20);
namex = files(i).name;
sss=regexp(namex, '(?:\d{4})\>','match');
format longg;
hm(x)=str2double(sss);
end
2 Kommentare
David Hill
am 20 Sep. 2020
I don't understand what you are trying to do. Please explain with an example (input and expected output). The double of '0000' is 0.
Akzeptierte Antwort
Star Strider
am 20 Sep. 2020
It is not possible to retain leading zeros in a numeric representation, such as double. To retain leading zeros, they must be kept as character arrays (single quotes) or string variables (double quotes).
.
2 Kommentare
Star Strider
am 20 Sep. 2020
The leading zeros would disappear if your converted them to numeric values.
out = compose("%04d", 0:5:20).'
DT = datetime(out, 'InputFormat',"HHmm", 'Format','HH:mm')
producing:
out =
5×1 string array
"0000"
"0005"
"0010"
"0015"
"0020"
DT =
5×1 datetime array
00:00
00:05
00:10
00:15
00:20
The ‘DT’ vector would then be saved as a datetime array (call it anything you wish), with the leading zeros intact, and converted to hours and minutes as well. (The 'InputFormat' string specifies how the input is parsed, and the 'Format' string specifies how it is displayed.)
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!