Store cell arrays composed of datetimes and numbers

Description & Goal. I have several cell arrays composed of datetimes "dt" and numbers/values "n", similar to this one:
[cellstr(dt) num2cell(n)]
ans =
1×2 cell array
{'19-Jun-2021 11:00:00'} {[20]}
where
>> dt
dt =
datetime
19-Jun-2021 11:00:00
>> n
ans =
20
Both datetimes and numbers contained in those cell arrays change everytime, and are "generated" inside a loop for.
My goal is to store all the different cell arrays, obtained inside the loop for, as matrix rows.
My Attempt. I tried to store those cell arrays by indexing with round brackets (if possible I would like to avoid curly brackets), but it does not work:
for i = 1 : 5
% different "datetime" and "numbers" are generated at the beginning of the loop
store(i,:) = [cellstr(dt) num2cell(n)]
end
Question & Desired Output. How can I get an output like this one ?
>> store
store =
19-Jun-2021 11:00:00 20
20-Jun-2021 19:00:00 44
21-Jun-2021 17:00:00 13
...

2 Commenti

"I have a cell array composed of datetimes and numbers/values"
Your example show a cell array containing a character vector, not a datetime object. It is unclear why you convert a perfectly good datetime object to a less versatile character vector.
Your loop works for me (with suitable preallocation):
store = cell(5,2); % preallocate!
for i = 1:5
dt = datetime(2022,randi([1,12]),1);
n = randi([0,99]);
store(i,:) = [cellstr(dt),num2cell(n)];
end
display(store)
store = 5×2 cell array
{'01-Jun-2022'} {[73]} {'01-Sep-2022'} {[50]} {'01-May-2022'} {[46]} {'01-Aug-2022'} {[69]} {'01-Jun-2022'} {[99]}
Sim
Sim il 10 Ago 2022
Modificato: Sim il 10 Ago 2022
Oh thanks a lot!
About "your examples show a cell array containing a character vector, not a datetime object", yes my fault... :-) Yes, I agree, datetime is a very good object.. :-) ...btw, if you copy and paste your reply in the "Answer" box I will accept it ;-)

Accedi per commentare.

 Risposta accettata

Your loop works for me (with suitable preallocation):
store = cell(5,2); % preallocate!
for i = 1:5
dt = datetime(2022,randi([1,12]),1);
n = randi([0,99]);
store(i,:) = [cellstr(dt),num2cell(n)];
end
display(store)
store = 5×2 cell array
{'01-Jul-2022'} {[27]} {'01-Dec-2022'} {[22]} {'01-Mar-2022'} {[24]} {'01-Jun-2022'} {[ 5]} {'01-Dec-2022'} {[61]}
If you only have a handful of columns, I would probably just allocate using curly-brace indexing:
store = cell(5,2); % preallocate!
for i = 1:5
store{i,1} = datetime(2022,randi([1,12]),1);
store{i,2} = randi([0,99]);
end
display(store)
store = 5×2 cell array
{[01-Oct-2022]} {[62]} {[01-Jul-2022]} {[71]} {[01-Apr-2022]} {[ 3]} {[01-Oct-2022]} {[56]} {[01-Jan-2022]} {[53]}
Have you considered using a table to store your data? That would mean that you get the benefits of keeing data in arrays of its native class, rather than splitting your data into lots of scalar arrays in a cell array:
tbl = table('Size', [5,2], 'VariableTypes', {'datetime', 'double'},'VariableNames',{'d','n'});
for i = 1:5
tbl.d(i) = datetime(2022,randi([1,12]),1);
tbl.n(i) = randi([0,99]);
end
display(tbl)
tbl = 5×2 table
d n ___________ __ 01-May-2022 19 01-Nov-2022 1 01-Oct-2022 90 01-Nov-2022 83 01-Sep-2022 91

3 Commenti

Have you considered using a table to store your data?
A table array, assuming that the values of n for each row are the same size, is a good idea. Since this data is time and/or date based, I think a timetable might be an even better idea. Timetables have certain date and time based functionality that tables do not provide, such as the ability to select rows based on a time interval using a timerange or the ability to change the time basis using retime or synchronize.
thanks for the comment @Steven Lord! :-)
thanks a lot @Stephen23 for your solution :-)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Richiesto:

Sim
il 10 Ago 2022

Commentato:

Sim
il 10 Ago 2022

Community Treasure Hunt

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

Start Hunting!

Translated by