如何在Matlab上将字符串数据转换为Int变量?
我有一个textdata{}行总数540万,带有3-4位整数。我希望在MATLAB上将它们转换为int如何在Matlab上将字符串数据转换为Int变量?,matlab,Matlab,我有一个textdata{}行总数540万,带有3-4位整数。我希望在MATLAB上将它们转换为int 我试着使用x=str2num(total_data(1:end,:),但不起作用 要返回整数,必须在字符串中添加前缀和后缀 b = cellfun(@(x)str2double(x), total_data); >> c = '1234567' c = 1234567 >> class(c) ans = char >> result = str
我试着使用
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