Replace column with text element into integers
1 view (last 30 days)
Show older comments
Alberto Basaglia
on 8 Jul 2020
Commented: Alberto Basaglia
on 9 Jul 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 Comments
Accepted Answer
madhan ravi
on 8 Jul 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)
More Answers (0)
See Also
Categories
Find more on String in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!