Cell in table data type conversion

4 次查看(过去 30 天)
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