filtering excel commands in matlab
8 次查看(过去 30 天)
显示 更早的评论
Hello, i have the attched file .
i want using script command to extract the table and filter the rows of the table where the dogs are.
so it basicky out of my excel table i need to get 3D array of of 1X1X2 (animal,size,Color)
dog M brown
dog M red
is there an effective way to do it in matlab commands?
Thanks.
0 个评论
回答(2 个)
dpb
2022-9-10
编辑:dpb
2022-9-11
tA=readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1121415/animals.xlsx');
tA=convertvars(tA,@iscellstr,'categorical');
tA
Now we've got the table, don't make piecemeal of it, use the variables of interest as grouping variables instead...
groupsummary(tA,'Animal')
When you subsequently add other data such as weights, ages, etc., etc., ... then you can compute statistics or whatever the same way with whatever functions you need.
tA.Animal=='dog'% see the logical addressing vector
tD=tA(tA.Animal=='dog',{'size','color'})
illustrates picking only the non-grouping variables since 'Animal' is now superfluous. To continue to carry it along anyway, use ":" for the column addressing vector.
2 个评论
dpb
2022-9-11
In general, it's wrong approach to physically separate tables unless it really is known that aren't going to use any of the other data for further analysis, but if you're adamant --
tDog=tA(tA.Animal=='dog',:);
Star Strider
2022-9-10
I have no idea what you want or the reason a 3D matrix is necessary.
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1121415/animals.xlsx')
T1u = unstack(T1,'size','Animal')
Different results are available with different argument permutations and more arguments as described in Name-Value Arguments.
.
4 个评论
Star Strider
2022-9-11
Try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1121415/animals.xlsx')
Dogs = T1(strcmpi(T1.Animal,'dog') & strcmpi(T1.color,'red'),:)
.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!