Double to cell conversion difficulties

2 次查看(过去 30 天)
Joe
Joe 2012-1-26
I have a cell array called temp which stores two columns of dates. Each date is currently stored as a char string (eg: 28/12/2808) within a cell. I'd like to convert these to datenumbers for plotting and sorting.However, when I do this:
temp(:,1)=datenum(temp(:,1),'dd/mm/yy');
I get:
"Conversion to cell from double is not possible."
I've tried drinking several cups of coffee but that hasn't helped either. I assume I am missing { } somewhere but I just can't figure out where. num2cell seems to work but it's not very elegant.

回答(2 个)

Bård Skaflestad
Bård Skaflestad 2012-1-26
There is a slight misunderstanding in the way you use datenum. Specifically, datenum(temp(:,1),'dd/mm/yy') returns a double for each element in the cell array temp(:,1). However, you cannot directly assign individual double|s to the corresponding element of |temp(:,1).
One way to achieve the (presumably) desired effect is to use the num2cell function like
temp(:,1) = num2cell(datenum(temp(:,1), 'dd/mm/yy'))
but there may be other techniques that solve the larger problem you're trying to solve more directly.

Jan
Jan 2012-1-26
When you want to use the date numbers for plotting and sorting, they must be stored as a numerical vector:
temp2(:,1) = datenum(temp(:,1), 'dd/mm/yy');

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by