Cell in table data type conversion

3 views (last 30 days)
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.

Accepted Answer

Chunru
Chunru on 4 May 2022
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

More Answers (1)

Walter Roberson
Walter Roberson on 4 May 2022
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 Comments
Mehdi Jaiem
Mehdi Jaiem on 4 May 2022
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 on 4 May 2022
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]}

Sign in to comment.

Categories

Find more on Data Type Conversion 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!