フィルターのクリア

Replace column with text element into integers

3 ビュー (過去 30 日間)
Alberto Basaglia
Alberto Basaglia 2020 年 7 月 8 日
コメント済み: Alberto Basaglia 2020 年 7 月 9 日
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!

採用された回答

madhan ravi
madhan ravi 2020 年 7 月 8 日
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)
  1 件のコメント
Alberto Basaglia
Alberto Basaglia 2020 年 7 月 9 日
It works! Thank you!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by