Cell in table data type conversion

4 visualizaciones (últimos 30 días)
Mehdi Jaiem
Mehdi Jaiem el 4 de Mayo de 2022
Comentada: Walter Roberson el 4 de Mayo de 2022
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.

Respuesta aceptada

Chunru
Chunru el 4 de Mayo de 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

Más respuestas (1)

Walter Roberson
Walter Roberson el 4 de Mayo de 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 comentarios
Mehdi Jaiem
Mehdi Jaiem el 4 de Mayo de 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 el 4 de Mayo de 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]}

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by