Replace column with text element into integers
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Alberto Basaglia
le 8 Juil 2020
Commenté : Alberto Basaglia
le 9 Juil 2020
Hi all!
I have the matrix below (imported from a .txt file):
'pippo' 1 2
'pluto' 3 4
'minnie' 5 6
'pippo' 7 8
I would like to replace the cells with text into integers, like this:
'pippo' = 1
'pluto' = 2
'minnie' = 3
After looking at other answers in the MATLAB forum I came up with this non-working script:
CARTIS=readtable('try.txt');
a=find(strcmp([CARTIS{:,1}],'pippo'));
CARTIS(a,1)={'1'};
CARTIS(a,1)=table2array(CARTIS(a,1));
CARTIS(a,1)=cell2mat(CARTIS(a,1));
CARTIS(a,1)=str2num(CARTIS(a,1));
Could someone please explain how to fix it, please?
Thank you!
0 commentaires
Réponse acceptée
madhan ravi
le 8 Juil 2020
T = readtable('try.txt');
T(:,1) = regexprep(T{:,1},'''','')
T.Var1 =(strcmp(T{:, 1},'pippo') * 1 + strcmp(T{:, 1},'pluto') * 1 + strcmp(T{:, 1},'minnie') * 3)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical 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!