How to Fix Cannot convert data of class duration into cell array in MATLAB?

S
support · Jul 23, 2026 · 0 views
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?
Expert Answer
Profile picture of Neeta Dsouza
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

Get a Free Consultation or a Sample Assignment Review!