Add new "datetime" elements to an existing cell array (matrix)

What I have so far:
a = cell(3,3);
index = [1 1
2 1
3 3];
b = datetime({'00:01:35'
'00:01:11'
'00:04:21'});
idx = sub2ind(size(a),index(:,1),index(:,2))
idx = 3×1
1 2 9
a(idx) = num2cell(b)
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21]}
What I need / Desired output: add further elements to the cell array (matrix) "a".
% For example, add the new element "datetime({'00:01:01'})"
% to the cell "index = [3 3]", i.e. "idx = 9":
a =
3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double }
{[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double }
{0×0 double } {0×0 double} {[24-Oct-2022 00:04:21], [24-Oct-2022 00:01:01]}
My attempt:
new_element = datetime({'00:01:01'});
a(9) = a + num2cell(new_element)
Operator '+' is not supported for operands of type 'cell'.

 Accepted Answer

I am not certain what a more universal end result would look liks, however this seems to work for this problem —
a = cell(3,3);
index = [1 1
2 1
3 3];
b = datetime({'00:01:35'
'00:01:11'
'00:04:21'});
idx = sub2ind(size(a),index(:,1),index(:,2))
idx = 3×1
1 2 9
a(idx) = num2cell(b)
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21]}
new_element = datetime({'00:01:01'})
new_element = datetime
24-Oct-2022 00:01:01
a{9} = [a{9} new_element] % Concatenate
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21 24-Oct-2022 00:01:01]}
The operation is a simple concatenation, using the [] concatenation operators.
.

6 Comments

Thanks a lot @Star Strider ! :-)
But, what if I have 2 or more elements to add to the cell array "a" ? Can I still use the concatenation ?
For example, if I want to add one new element to the cell "8" and one new element to the cell "9", i.e. ?
new_elements = datetime({'00:01:01' ...
'00:01:56'});
a{[8 9]} = [a{[8 9]} num2cell(new_elements)]
I get an error
Error using datetime/horzcat
All inputs must be datetimes or date/time character vectors or date/time strings.
Error in untitled (line 14)
a{[8 9]} = [a{[8 9]} num2cell(new_elements)]
As always, my pleasure!
The second num2cell call is not necessary, since ‘new_element’ or ‘new_elements’ do not need to be cell arrays themselves. Also, in my tweak to your original code, the previous value stored in ‘a(9)’ is retrieved as ‘a{9}’ allowing the concatenation to proceed.
The previous concatenation approach works here as well —
a = cell(3,3);
index = [1 1
2 1
3 3];
b = datetime({'00:01:35'
'00:01:11'
'00:04:21'});
idx = sub2ind(size(a),index(:,1),index(:,2))
idx = 3×1
1 2 9
a(idx) = num2cell(b)
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21]}
new_elements = datetime({'00:01:01' ...
'00:01:56'});
a{9} = [a{9} new_elements] % Concatenate
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21 24-Oct-2022 00:01:01 24-Oct-2022 00:01:56]}
Since I am not exactly certain where this is starting from, confirming this —
a = cell(3,3);
index = [1 1
2 1
3 3];
b = datetime({'00:01:35'
'00:01:11'
'00:04:21'});
idx = sub2ind(size(a),index(:,1),index(:,2))
idx = 3×1
1 2 9
a(idx) = num2cell(b)
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21]}
new_element = datetime({'00:01:01'})
new_element = datetime
24-Oct-2022 00:01:01
a{9} = [a{9} new_element] % Concatenate
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21 24-Oct-2022 00:01:01]}
new_elements = datetime({'00:01:01' ...
'00:01:56'});
a{9} = [a{9} new_elements] % Concatenate
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21 24-Oct-2022 00:01:01 24-Oct-2022 00:01:01 24-Oct-2022 00:01:56]}
view_a_9 = a(9) % Confirm Result
view_a_9 = 1×1 cell array
{[24-Oct-2022 00:04:21 24-Oct-2022 00:01:01 24-Oct-2022 00:01:01 24-Oct-2022 00:01:56]}
Repeated concatenations work as well, duplicating the original second datetime value, if desired.
.
Thanks a lot for your reply @Star Strider :-) Very kind!
However, I meant something slighlty different, i.e. to add two new elements at the same time into two different cells, and not into the same cell as you have shown (cell [3 3])....
I try to write down what I meant.... and sorry for not having explained clearly, my fault ! :-) ....and, please let me know if I need to create a new question in Matlab Ask...
% (1) create the cell array / matrix "a"
a = cell(3,3);
% (2) add the first elements to the cell array / matrix "a"
index = [1 1
2 1
3 3];
b = datetime({'00:01:35'
'00:01:11'
'00:04:21'});
idx = sub2ind(size(a),index(:,1),index(:,2));
a(idx) = num2cell(b)
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21]}
Here below I show what I need, but without the loop for (if possible)!
% (3) add two new elements:
% - one new element inside the cell [2 3]
% - one new element inside the cell [3 3]
index2 = [2 3
3 3];
new_elements = datetime({'00:01:01'
'00:01:56'});
idx2 = sub2ind(size(a),index2(:,1),index2(:,2));
for i = 1 : size(idx2,1)
a{idx2(i)} = [a{idx2(i)} new_elements(i)];
end
a,
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {[24-Oct-2022 00:01:01 ]} {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21 24-Oct-2022 00:01:56]}
The loop (or some version of it) is likely the only way to do what you want. All my attempts at using multiple indexing to do it with the concatenation failed.
.
thanks a lot @Star Strider, very very kind!!! :-) Your efforts are very appreciated!
(I have just opened a new question about this issue, but maybe better to close it...)

Sign in to comment.

More Answers (0)

Categories

Asked:

Sim
on 24 Oct 2022

Commented:

on 24 Oct 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!