0

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,

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

No glory for seconds in the matlab race!
If you're interested in date strings specifically, datestr is the best tool for the job. E.g. it allows localization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.