Store cell arrays composed of datetimes and numbers
Mostra commenti meno recenti
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)
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su MATLAB in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!