Mushi
Mushi

Reputation: 367

Concatenate variable horizontally to make one variable in matlab

Is it possible to concatenate multiple variable horizontally to make a single variable in Matlab?

For Example, I want to join:

year = 2001, month = 06, day = 15

to make one variable '20010615' which I could search in a matrix.

I hope I am clear.

Regards,

Upvotes: 0

Views: 114

Answers (1)

Shai
Shai

Reputation: 114806

If you want a string output, use string formatting and sprintf

sprintf('%04d%02d%02d', year, month, day );

If you want a numeric output, simply multiply

day + 100 * month + 10000 * year

Update:

Thanks to @Joshua's comment: if you are indeed working with date/time information you should also look into datestr that allows more speciallized formatting for date and time information.

Upvotes: 4

Related Questions