Maddy
Maddy

Reputation: 2570

Convert string array to cell

I need to output a cell to an excel file. Before this I need to convert a date column of integers to datestrings. I know how to do this, but I am not able to put this new string array back into the cell -

mycell = { 'AIR' [780] [1] [734472] [0.01] ; ...
           'ABC' [780] [1] [734472] [0.02]}

I did this -->

dates = datestr(cell2mat(mycell(:,4))) ;

What I need as an answer is:

{'AIR' [780] [1] '14-Dec-2010' [0.01] ;
 'ABC' [780] [1] '23-Dec-2010' [0.03] ; }

so that I can now send it to an excel file using xlswrite.m

Upvotes: 3

Views: 12120

Answers (2)

gnovice
gnovice

Reputation: 125874

One alternative that avoids the conversions is to use the function CELLFUN:

mycell(:,4) = cellfun(@datestr,mycell(:,4),'UniformOutput',false);
%# Or an alternative format...
mycell(:,4) = cellfun(@(d) {datestr(d)},mycell(:,4));

Both of the above give the following result for your sample cell array:

mycell = 

    'AIR'    [780]    [1]    '30-Nov-2010'    [0.0100]
    'ABC'    [780]    [1]    '30-Nov-2010'    [0.0200]

Upvotes: 1

zellus
zellus

Reputation: 9582

mycell = { 'AIR' 780 1 734472 0.01] ; ...
           'ABC' 780 1 734472 0.02]}

mycell(:,4) = cellstr(datestr(cell2mat(mycell(:,4))))

mycell = 

    'AIR'    [780]    [1]    '30-Nov-2010'    [0.01]
    'ABC'    [780]    [1]    '30-Nov-2010'    [0.02]

Upvotes: 3

Related Questions