2

I have a cell array that I call "Table", as in the code below (but my array has more lines). Column 1 contains dates in string format. I want to add an additional column that contains the dates in datetime format. I did the following, which works, but it is VERY slow. What are the alternatives?

% Table that I have:

Table{1,1} = 'Stringdate';
Table{2,1} = '01.01.1999';
Table{3,1} = '02.01.1999';
Table{4,1} = '03.01.1999';
Table{5,1} = '04.01.1999';

% What I want to add:

Table{1, size(Table,2)+1} = 'Datetime';

for index = 2:length(Table)
    Table{index, size(Table,2)} = datetime(Table{index, 1});
end
1
  • The code runs perfectly on my machine?! Feel free to change it if it doesn't work for you and thanks in advance for any help Commented Feb 4, 2018 at 11:54

1 Answer 1

3

You can apply datetime to all of them in one-go and use just num2cell and indexing to achieve the same result as that of your loop.

Table(2:end,2) = num2cell(datetime(Table(2:end,1)));
%You might need to specify the InputFormat as well i.e.
%Table(2:end,2) = num2cell(datetime(Table(2:end,1),'InputFormat','dd.MM.yyyy'));
Sign up to request clarification or add additional context in comments.

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.