extract numbers from cells

3 vues (au cours des 30 derniers jours)
alessio tugnolo
alessio tugnolo le 11 Juin 2021
Commenté : alessio tugnolo le 11 Juin 2021
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 commentaire
KSSV
KSSV le 11 Juin 2021
Read about regexp

Connectez-vous pour commenter.

Réponses (2)

Chunru
Chunru le 11 Juin 2021
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 commentaire
alessio tugnolo
alessio tugnolo le 11 Juin 2021
Great! It works beautifully!
Thanks

Connectez-vous pour commenter.


Stephen23
Stephen23 le 11 Juin 2021
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
  1 commentaire
alessio tugnolo
alessio tugnolo le 11 Juin 2021
Thank you !

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numerical Integration and Differential Equations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by