0

At the moment I am using xlsread to open a set of data that I have in excel with given timestamps. But when these values are placed in matlab it changes the formatting of the timestamp. In excel it is: dd/mm/yyyy HH:MM

but when it puts it into matlab it changes it to

mm/dd/yyyy HH:MM

which ruins my other code. I have tried using formatIn and specifying it, but then it returns an error if no value for midnight is given.

Any help would be appreciated.

2
  • What code do you use? In what format do you have those timestamps in Matlab? Cell array of strings? Commented Aug 25, 2014 at 11:07
  • Yeah, was a cell array of strings. Commented Aug 26, 2014 at 5:05

2 Answers 2

1

You can use datenum and datestr to convert the format to what you want. In the following example I'm assuming your timestamps are contained in a cell array of strings, but it also works if it's a char matrix:

>> timestamps = {'08/25/2014 13:14'; '08/26/2014 14:15'} %// mm/dd/yyyy HH:MM
>> result = datestr(datenum(timestamps, 'mm/dd/yyyy HH:MM'), 'dd/mm/yyyy HH:MM')
result =
25/08/2014 13:14
26/08/2014 14:15
Sign up to request clarification or add additional context in comments.

4 Comments

If you want to enforce a standard date format, I would recommend this method. However if the excel contains an unusual date format and that is required, consider just using the date representation directly.
Thats what I originally had, specify the format directly using datenum but it doesn't like it when no HH:MM is given. Matlab imports 00:00 being midnight as a blank which is simply the default. It chucks an error because of this. Not sure how to fix it.
It should default to 0 if no value is given but my code is not doing this. :/
So when I go >> datenum('1/01/2011', 'mm/dd/yyyy HH:MM') It throws an error instead of defaulting to zero like it should.
0

What Luis recommended should help you to get any format that you like. However there is something important to realize here:

Excel does not 'have' the date in your format. It has the date stored as a number like 123546.123 and presents it to you in a certain way.

If you want to get the date in exactly the way that excel presents it, the trick is to avoid importing the relevant column as a date, but just import it as text instead.

How to do this depends on your import method, but it should not be very hard.

Comments

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.