accessing table data in different forms

1 次查看(过去 30 天)
Hi I have T table as follows.
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
FirstName = {'Amanda' ;'Brenda';'Carl'; 'Denis'; 'Ethan'};
Something = {'String1' ;'String2';'String2'; 'String1'; 'String5'};
Weight = [176;163;131;133;119];
FavoriteColor = {'blue' ;'red' ;'yellow'; 'orange' ;'colorblind' };
T = table(Age,FirstName,Weight,FavoriteColor,Something,'RowNames',LastName)
T.FavoriteColor= categorical(T.FavoriteColor);
T.Something= categorical(T.Something);
when I use
A=T(:,5);
I get a variable A which is a table as well. But when I use
A=T.Something;
I get a variable A which is a categorical value like the ones is column Something. I want to use loops so I need to use the first one with indices but I want the result in the second one. What should I do?
  2 个评论
Geoff Hayes
Geoff Hayes 2014-10-25
Ege - what are you trying to achieve? You mention that you want to use loops, but what do you want to use the loops for?
Ege
Ege 2014-10-25
Well I just started the project I will use it for many things but for now, I have divided my dataset into 2 tables which holds categorical and non categorical data. I'm currently using the categorical one and try to implement a histogram on that since usual histogram function does not work on categorical data. My dataset is huge so at first I will use the categorical column of the T table I mentioned in the question. When I use it like below, it works fine.
a=T.Something;
hist=zeros(size(a,1),1);
u=unique(a);
for i=1: size(a,1)
for j=1: size(u,1)
if isequal(a(i),u(j))
hist(j)=hist(j)+1;
end
end
end
for i=1:numel(hist,1)
if isequal(hist(i),0)
hist(i)=[];
end
end
histogram = table(u(:),hist(:));
But when I try using
a=T(:,5);
the histogram does not work I get wrong results in hist().

请先登录,再进行评论。

采纳的回答

Ege
Ege 2014-10-26
accesing it like
a=T{:,5};
solved my problem. Hope this helps someone as well :D

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by