extract numbers from cells

조회 수: 3 (최근 30일)
alessio tugnolo
alessio tugnolo 2021년 6월 11일
댓글: alessio tugnolo 2021년 6월 11일
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?
  댓글 수: 1
KSSV
KSSV 2021년 6월 11일
Read about regexp

댓글을 달려면 로그인하십시오.

답변 (2개)

Chunru
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
g = 3×1
2000000 500000 20000000
  댓글 수: 1
alessio tugnolo
alessio tugnolo 2021년 6월 11일
Great! It works beautifully!
Thanks

댓글을 달려면 로그인하십시오.


Stephen23
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)
V = 3×1
2000000 500000 20000000

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by