Convert cell into double

5 ビュー (過去 30 日間)
julro
julro 2014 年 6 月 23 日
回答済み: Star Strider 2014 年 6 月 23 日
Hi,
I have a matrix (1050*7) where I want to convert the cell format into a double:
It looks like that:
'0101' '2' '31.1.2013' '5' '5' '1' '23875' '0101' '2' '31.1.2013' '3' '3' '1' '5437' ...
Thanks already!

回答 (5 件)

Dishant Arora
Dishant Arora 2014 年 6 月 23 日
編集済み: Dishant Arora 2014 年 6 月 23 日
If your data follow this strict order and the 3rd column refers to date, try this:
function y = fun(x)
if isnan(str2double(x))
y = datenum(regexprep(x , '\.' , '-'))
else
y = str2double(x)
end
% consider testCell as your input cell, run this script
outputMatrix = cellfun(@(x)fun(x) , testCell , 'Un' , 1);

Julia
Julia 2014 年 6 月 23 日
There is the command cell2mat (from the Matlab help):
Combine matrices in the four cells of cell array c into matrix m.
c = {[1], [2, 3, 4];
[5; 9], [6, 7, 8; 10, 11, 12]};
m = cell2mat(c)
m is a 3-by-4 matrix:
m =
1 2 3 4
5 6 7 8
9 10 11 12

Ben11
Ben11 2014 年 6 月 23 日
If all your entries are numbers you can use cell2mat. Is that the case?

julro
julro 2014 年 6 月 23 日
Thanks for your response. I tried it but I am getting the error:
all input cell arrays must be same data type. Might be due to the '31.1.2013' in the 3. column...? So that relates to you question - no, not all are numbers.
Best,
Julia

Star Strider
Star Strider 2014 年 6 月 23 日
It looks like a codes ( e.g. '0101' ) and dates ( e.g. '31.1.2013' ) so a simple conversion is probably not what you want. I would convert it to a table with the cell2table function if your version of MATLAB supports tables.

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by