我正在閱讀一個excel文件。在其中一列(只包含很長的數字)中,MATLAB將其導入爲CELL(因爲有一個標題)。MATLAB轉換:CELL to DOUBLE;但數字變化
這是進口的樣品:
'980000684210053338'
這是我的嘗試:
轉換爲DOUBLE,但數字變化
tableM.v1 = cellfun(@str2num,tableM.v1);
轉換爲DOUBLE,但數字變化
tableM.v1 = cellfun(@str2double,tableM.v1);
轉換爲CHAR但數字正確
tableM.v1 = cell2mat(tableM.v1);
如何在保持正確值的同時將此CELL轉換爲DOUBLE?
謝謝
P.S.我正在使用MATLAB R2016a。
UPDATE:
我跑到離回答了這個代碼:
tableM.v1 = vpa(tableM.v1); % assuming tableM.v1 is a cellstr
我收到此錯誤:
Warning: Support of strings that are not valid variable names or define a number will be removed in a future release. To create symbolic expressions, first create symbolic variables and then use operations on them.
In sym>convertExpression (line 1536)
In sym>convertChar (line 1441)
In sym>tomupad (line 1198)
In sym (line 177)
In cell2sym (line 28)
In sym>tomupad (line 1208)
In sym (line 177)
In vpa (line 44)
Error using symengine
New array must have the same number of elements as the original array.Error in sym/reshape (line 50)
ySym = mupadmex('symobj::reshape',x.s,args{:});Error in cell2sym (line 34)
S = reshape(sym([Csym{:}]), size(C));Error in sym>tomupad (line 1208)
xsym = cell2sym(x);Error in sym (line 177)
S.s = tomupad(x);Error in vpa (line 44)
ss = sym(s);
不錯+1。我很驚訝地發現MATLAB還沒有'str2int *'套件。內部的「雙重」鑄造也是非常奇怪和煩人的。我注意到,出於某種原因,將VPA非標量轉換爲'uint64'時也會發生這種情況:'str ='980000684210053338'; disp(uint64(vpa(str))); disp(uint64(vpa(repmat({str},1,2))));'產生不同的結果,這很荒謬。 – TroyHaskin
這似乎工作,但一個小問題。它將'0,0090'轉換爲'90'或'3,5610'爲'305610'。 – PeyM87
您的示例不包含數字以外的字符。這就是爲什麼我添加了關於錯誤檢查的最後一個警告。該函數還假定您的輸入已經是整數,而不是浮點數,因爲您沒有指出它是一個選項。這將需要一些額外的步驟來搜索小數點指示符,然後截斷數字。 – craigim