accessing table data in different forms

2 Ansichten (letzte 30 Tage)
Ege
Ege am 25 Okt. 2014
Beantwortet: Ege am 26 Okt. 2014
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 Kommentare
Geoff Hayes
Geoff Hayes am 25 Okt. 2014
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 am 25 Okt. 2014
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().

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by