convert array string data in array serial number

this example clarify my problem
>> size(B)
ans =
463628 1
>> B(1)
ans =
"01/01/2008"
>> datenum(B(1,:),'dd/mm/yyyy')
ans =
733408
>> datenum(B,'dd/mm/yyyy')
Error using datenum
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed to convert from text to date number.
i can convert the single string but not the array of strings

댓글 수: 6

@Luca Re: please save your data in a MAT file and upload it by clicking the paperclip button.
dataan.m
@Luca Re: lots of the DATAAN string elements are empty, i.e. have no characters:
Clearly empty text does not match the specified date format and will fail when you try to parse it with DATENUM.
shamal
shamal 2023년 5월 23일
편집: shamal 2023년 5월 23일
thank..
they are empty because I have preallocated the variable and not all array vectors have the same number of elements
B=strings(righe,colum);
What do you recommend if I have empty array elements? fill them with false dates for the command to work correctly
"What do you recommend if I have empty array elements?"
Use DATETIME instead of deprecated DATENUM:
B = ["01/01/2008";""]
B = 2×1 string array
"01/01/2008" ""
D = datetime(B, "InputFormat","dd/MM/yyyy")
D = 2×1 datetime array
01-Jan-2008 NaT
yes but i want do preallocate it
is it correct to do it like this? (i need 5600 element in this matrix and I have to assign values ​​read from .txt
dataan=NaT(righe,colum);

댓글을 달려면 로그인하십시오.

답변 (1개)

Unless you're using an older release where it is not available I strongly recommend you use datetime instead of serial date numbers. Note I've changed your B slightly to make it clear that datetime is importing it correctly, not confusing the day and month information.
B = "02/03/2008";
dt = datetime(B, "InputFormat", "dd/MM/yyyy")
dt = datetime
02-Mar-2008
With a datetime array a lot of operations are easier and easier to interpret than with a list of date numbers. As an example, what's the day after dt?
nextDay = dt + days(1)
nextDay = datetime
03-Mar-2008

댓글 수: 3

v =
"01/01/2008"
>> datetime(v,'dd/mm/yyyy' )
Error using datetime
Wrong number of arguments.
i've problem to use it
"i've problem to use it"
It will work when you read and follow the DATETIME documentation or the example that Steve Lord showed you. Why did you change the syntax to something completely different?
S = "01/01/2008";
D = datetime(S, 'InputFormat','dd/MM/yyyy')
D = datetime
01-Jan-2008
thanks..I mess around a bit with these formats

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2023년 5월 23일

댓글:

2023년 5월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by