2014-01-20 33 views
0

我有一個矩陣有兩列,值和類型。 我要檢查數據如何檢查單元格數組中的值的類型?

m1 = {'Value','Type' 
     'str','char'; 
     'stra','Real'; 
     '34','char'; 
     '2','Bool'; 
     '1','Bool' 
    } 

如何返回,並非對應的類型(卷,焦炭,布爾)值的類型。 結果將會是:

mError = 'stra' 
     '2' 

謝謝。

問候

+0

將 '34' 遵守爲' char'類型? –

回答

2

如果任何字符串爲 '焦炭' 可以接受的:

ind1 = strcmp(m1(:,2),'Real') & isnan(str2double(m1(:,1))); 
%// Any string is acceptable as 'char', so no ind2 
ind3 = strcmp(m1(:,2),'Bool') & ~ismember((str2double(m1(:,1))),[0 1]); 
mError = m1(ind1|ind3,1) 

如果數字字符串不爲 '焦炭' 可以接受的:

ind1 = strcmp(m1(:,2),'Real') & isnan(str2double(m1(:,1))); 
ind2 = strcmp(m1(:,2),'char') & ~isnan(str2double(m1(:,1))); 
ind3 = strcmp(m1(:,2),'Bool') & ~ismember((str2double(m1(:,1))),[0 1]); 
mError = m1(ind1|ind2|ind3,1) 
+0

是的,任何字符串都可以接受爲'char' – user3214383

+0

可以檢查十進制值嗎?我試過這個:find(round(m1(:1)== mat(:,1)))沒有成功 – user3214383

+0

你是什麼意思與「檢查十進制值」? –