필터 지우기
필터 지우기

Cell in table data type conversion

조회 수: 1 (최근 30일)
Mehdi Jaiem
Mehdi Jaiem 2022년 5월 4일
댓글: Walter Roberson 2022년 5월 4일
Greetings Suppose I have column of type table and the content of the cells has "yes" and "no". Instead I wand the content of the cells to be true or false with main class as boolean (not string or char). Furthermore i have to use cellfunc() to modify the column (cells)content. Using strcmp or replace is not what I seek here. Instead i want as mentioned to have a class type boolean.

채택된 답변

Chunru
Chunru 2022년 5월 4일
a = (1:3)';
b = {'yes', 'no', 'yes'}';
t = table(a,b)
t = 3×2 table
a b _ _______ 1 {'yes'} 2 {'no' } 3 {'yes'}
t.b = categorical(t.b)=='yes'
t = 3×2 table
a b _ _____ 1 true 2 false 3 true

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 5월 4일
a = (1:3)';
b = {'yes', 'no', 'yes'}';
t = table(a,b)
t = 3×2 table
a b _ _______ 1 {'yes'} 2 {'no' } 3 {'yes'}
t.b = cellfun(@(b) length(b) == 3, t.b)
t = 3×2 table
a b _ _____ 1 true 2 false 3 true
  댓글 수: 2
Mehdi Jaiem
Mehdi Jaiem 2022년 5월 4일
Thank you very much it worked !
is there also a function that converts cell content to numerical vectors? (data is a table)
data(:,78)=
{'[100019, 100003, 100005, 100016, 100007]'}
{'[100017]' }
{'[100001, 100012]' }
{'[100012]' }
{'[100016, 100018, 100008, 100007, 100001]'}
{'[100011, 100009]' }
{'[100005, 100001]' }
{'[100005, 100013]' }
{'[100006]' }
{'[100014]' }
{'[100006]' }
{'[100004, 100003, 100014, 100015, 100013]'}
{'[100008]' }
{'[100012, 100010]' }
{'[100012]' }
{'[100005, 100001]' }
{'[100002]' }
{'[100005]' }
{'[100001]' }
{0×0 char }
data(:,4)=cellfun(@str2num, data(:,4), 'UniformOutput', false);
but it seems like this is not the proper solution for converting the content of the cells to numerical vectors
Best regards
Walter Roberson
Walter Roberson 2022년 5월 4일
data78 ={
'[100019, 100003, 100005, 100016, 100007]'
'[100017]'
'[100001, 100012]'
'[100012]'
}
data78 = 4×1 cell array
{'[100019, 100003, 100005, 100016, 100007]'} {'[100017]' } {'[100001, 100012]' } {'[100012]' }
try1 = cellfun(@str2num, data78, 'uniform', false)
try1 = 4×1 cell array
{[100019 100003 100005 100016 100007]} {[ 100017]} {[ 100001 100012]} {[ 100012]}
try2 = cellfun(@str2double, regexp(data78, '\d+', 'match'), 'uniform', 0)
try2 = 4×1 cell array
{[100019 100003 100005 100016 100007]} {[ 100017]} {[ 100001 100012]} {[ 100012]}

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by