2012-06-11 50 views
6

我有一個大小爲400x1的特定單元格。它基本上由字符串形式的數字組成。我的意思是,當我做將單元格轉換爲matlab中的數組

mycell{1} 

它給出的結果'1'

所以,你可以看到1號是字符串的形式。我怎樣才能將其轉換爲數組數組?

回答

5

像這樣如果size(mycell)是400x1。 。 。 。 。

str2num(cell2mat(mycell)) 

...或者是這樣,如果大小是1x400

str2num(cell2mat(mycell')) 

但是,這會造成問題,如果你的任何字符串包含不同數量的字符,即

mycell{1} = '2' 
mycell{2} = '33' 

如果您有這樣的情況,

str2double(mycell) 

...似乎處理這個確定正如在其他答案中提到的!

7
str2double(mycell) 

前提是你得的東西一個數組,看起來像雙打:

>> c = {'1' '2' ; '3' '4'} 

c = 

    '1' '2' 
    '3' '4' 

>> str2double(c) 

ans = 

    1  2 
    3  4 

>> whos ans 
    Name  Size   Bytes Class  Attributes 

    ans  2x2    32 double    

如果你有一些看起來並不像一個雙,你會得到在該單元格中NaN結果:

>> c{2,2} = 'aei' 

c = 

    '1' '2' 
    '3' 'aei' 

>> str2double(c) 

ans = 

    1  2 
    3 NaN 
+0

+1 - >這是不是我的答案:)原有的部分更普遍 – learnvst

3

您也可以嘗試cellfun(@str2num,mycell) 如果你有加倍的電池陣列:

mycell = {'1.56548524'; '1.5265'; '-4.2616' ;'-0.2154' ;'2.15'};

你可以嘗試

mat = cellfun(@str2num,mycell)

相關問題