Ladenkov Vladislav
Ladenkov Vladislav

Reputation: 1297

Converting numeric matrix column to datetime column

I have a 1000x5 double matrix:

79  735727,416666667    735727,452083333    735727,479166667    2
80  735727,441666667    735727,483333333    735727,506250000    9
81  735727,506944445    735727,534722222    735727,561111111    2
82  735727,541666667    735727,604861111    735727,648611111    9
83  735727,556944444    735727,572916667    735727,602083333    7

2rd, 3rd, 4th columns are dates. I want to replace their numerical representation with their datetime representation. I convert these columns like this:

arrive_date = datestr(data(:, 2))
arrive_date = datetime(arrive_date)

But, can't assign it back to the data's column:

data(:, 2) = arrive_date

Error: The following error occurred converting from datetime to double: Undefined function 'double' for input arguments of type 'datetime'. To convert from datetimes to numeric, first subtract off a datetime origin, then convert to numeric using the SECONDS, MINUTES, HOURS, DAYS, or YEARS functions.

Upvotes: 0

Views: 759

Answers (1)

nalyd88
nalyd88

Reputation: 5128

You can't store date objects in an array of doubles. You will need to use a cell array if you want to mix types. See the example below:

data = [79  735727.416666667    735727.452083333    735727.479166667    2;
        80  735727.441666667    735727.483333333    735727.506250000    9];
arrive_date = datestr(data(:, 2))
arrive_date = datetime(arrive_date)

% Convert to cell array (each column is contained in a cell)
data = num2cell(data,1);

data{2} = arrive_date

The output shows the new type (second cell is a vector of datetime objects).

data =

  1×5 cell array

    [2×1 double]    [2×1 datetime]    [2×1 double]    [2×1 double]    [2×1 double]

Upvotes: 1

Related Questions