extract numbers from cells
2 次查看(过去 30 天)
显示 更早的评论
I have a cell array with n rows wich contains:
'Gain=2M'
'Gain=500k'
'Gain=20M'
....
I would extract only the number which is contained in each cell. How can I do that?
回答(2 个)
Chunru
2021-6-11
s={'Gain=2M'
'Gain=500k'
'Gain=20M'};
n = length(s);
g = zeros(n, 1);
for i=1:n
tmp = strrep(s{i}, 'Gain=', '');
g(i) = str2num(tmp(1:end-1));
switch tmp(end)
case 'M'
g(i)=g(i)*1e6;
case 'k'
g(i)=g(i)*1e3;
end
end
g
Stephen23
2021-6-11
Simply download my FEX submission SIP2NUM:
and use it like this:
C = {'Gain=2M';'Gain=500k';'Gain=20M'};
V = cellfun(@sip2num,C)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!