Effacer les filtres
Effacer les filtres

Cell in table data type conversion

1 vue (au cours des 30 derniers jours)
Mehdi Jaiem
Mehdi Jaiem le 4 Mai 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.

Réponse acceptée

Chunru
Chunru le 4 Mai 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

Plus de réponses (1)

Walter Roberson
Walter Roberson le 4 Mai 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 commentaires
Mehdi Jaiem
Mehdi Jaiem le 4 Mai 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 le 4 Mai 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]}

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by