我有一個textdata {}行總數爲540萬與3-4位整數。我希望在MATLAB上將它們轉換爲int。如何將數據字符串轉換爲Matlab上的Int變量?
我試過使用x = str2num(total_data(1:end,:))
但它沒有工作。
我有一個textdata {}行總數爲540萬與3-4位整數。我希望在MATLAB上將它們轉換爲int。如何將數據字符串轉換爲Matlab上的Int變量?
我試過使用x = str2num(total_data(1:end,:))
但它沒有工作。
b = cellfun(@(x)str2double(x), total_data);
要返回整數,你必須添加一個前綴和後綴字符串...
>> c = '1234567'
c =
1234567
>> class(c)
ans =
char
>> result = str2num(c)
result =
1234567
>> class(result)
ans =
double
>> result = str2num(['int32(' c ')'])
result =
1234567
>> class(result)
ans =
int32
我會做這樣的事情:
%Test data
N = 1e4;
textdata = cell(N,1);
for ix = 1:N
textdata{ix} = num2str(ix);
end
%Convert to integers
dataAsInts = zeros(size(textdata),'int32');
for ix = 1:N
dataAsInts(ix) = int32(sscanf(textdata{ix},'%d'));
end