How do I reserve a size for my data so that zero's are not dropped or forgotten?

I am writing for determining what day of the week it is. I am using 'num2str' to create a string for my user 'year' entry and 'str2double' to recall the last two and first two digits separately. This method falls apart when I attempt to determine the day of the week when the year is very small. For example, 01/01/0003. Is there a way to store the zeros to make this work or do I have to write a lot of 'if' statements? If you know of another way besides the build in method, it would be appreciated.

5 Kommentare

Can you explain the "fall apart"? What values are you getting and what did you expect to get?
Greg
Greg am 11 Apr. 2013
Bearbeitet: Greg am 11 Apr. 2013
I have the following: Yr=num2str(Y); y=str2double(Yr(3:4)); c=str2double(Yr(1:2));
and my error is "Index exceeds matrix dimensions." when I try to have Y=0003. I am expecting it to use y=03 and c=00. My current program works for 3/1/1000 to 2/28/10000 but I would like to get it to also work from 03/01/0000
When you type Y = 0003, you are actually setting Y to 3 of double precision number. When 3 is converted to string, it is '3' with a length of 1.
If you really want to reserve zeros in front of a number so it is always 4 characters long when converting to string, do
Yr = num2str(Y, '%04i');
Thank you very much, that did the trick.

Antworten (1)

Use
datevec(datenum('01/02/0003'))
The output will be in the form of
[ Year Month Day Hour Minute Second ]

Diese Frage ist geschlossen.

Gefragt:

am 11 Apr. 2013

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by