Finding array elements that include a specified combination of values?

I have the following problem. I need to match two sets of dates in two separate arrays, but the formats are different (YYYYMM & YYYYMMDD), and in one of the arrays the dates are randomly mixed.
So I'd need a way to refer to specific parts of an element. As I have the correct timeline with YYYYMM dates, the easieast thing would be to somehow require the beginning of the YYYYMMDD element to match YYYYMM?
Thank you so much in advance!

 Akzeptierte Antwort

You can extract the parts of the date using
yourDate = '201105';
switch length(yourDate)
case 6
[Y,M] = datevec(yourDate,'yyyymm')
case 8
[Y,M,D] = datevec(yourDate,'yyyymmdd')
otherwise
error('Invalid date string length.')
end
Y =
2011
M =
5
The same sequence with yourDate = '20110514' gives
Y =
2011
M =
5
D =
14

7 Kommentare

Thank you for your help, but I'm afraid I don't actually understand your code (and can't make it work). Could you please explain it?
Sorry - I was a bit careless transposing my work. I have edited the above answer.
Thank you! I actually managed to find an alternative way to get the job done (a bit more manual work), but I believe I'll need this later.
I like helping people who help themselves!
I'm sorry, but I can't get it to work by reading in dateinformation from an array. I believe the trouble is that the dateinformation when it was imported was as an integer or double, so now matlab can't understand it...
I need the original dates in my study, but I just can't get it to work. With Import Wizard I don't get to set the dates, and for some reason I can't get fopen and textscan to work.
Is there a way to read the values as a string, so that i could thereafter convert them to datevectors.
Thank you a million if you have the patience to answer. I'm getting mildly annoyed with myself as it is.
Sorry for having bothered you, I managed to get around the problem by using the following code:
datum1=zeros(601,1);
datum2=zeros(601,1);
datum3=zeros(601,1);
for k=2:601
str= num2str(winners_aboveRF(k,1));
[Y,M,D] = datevec(str,'yyyymmdd');
datum1(k)=Y;
datum2(k)=M;
datum3(k)=D;
end
I know it's not classy, but it works, so I'm happy. If you however would happen to know a more elegant solution I'd be happy to hear it.
Thank you again for all your help!
Are you sure that your code works? What do you get for Y and M if you type [Y,M,D] = datevec('201112','yyyymmdd')?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by