Question
I am trying to store a MATLAB duration object inside a cell array using cellstr() or direct assignment, but MATLAB throws Error: Cannot convert data of class 'duration' into cell array. How do I properly convert duration arrays to strings or cell arrays?
Related Questions
Expert Answer
Neeta Dsouza
PhD Expert
Answered Aug 8, 2026
To convert a MATLAB duration array into a cell array of strings, convert the duration object to a string array first before wrapping it into a cell array, rather than passing the duration object directly into cellstr().
Solution Code:
% Create duration array [01:00:00, 02:00:00, 03:00:00]
d = hours(1:3);
% Convert duration to string array first, then to cell array
cellArray = cellstr(string(d));
disp(cellArray);
Alternatively, if you want a custom formatted string representation:
d.Format = 'hh:mm:ss';
cellArray = cellstr(char(d));
Have a different question? Ask here