Clear Filters
Clear Filters

grouping string values in matlab

3 views (last 30 days)
Ananya Malik
Ananya Malik on 20 Mar 2017
Commented: Guillaume on 20 Mar 2017
I have 3 columns. 1st column is a numeric (1,2,3....) denoting person number. 2nd column states the day of week(sun-sat). 3rd column consists location names (paris,london,italy...). I want to group the 3rd column based on 2nd column for a particular person, i.e. all the locations a particular person may visit on a particular day. Attached is a sample file containing both input and the required output. any help will be greatly appreciated

Accepted Answer

Guillaume
Guillaume on 20 Mar 2017
t = readtable('help.xls', 'Range', 'A3:C17', 'ReadVariableNames', false);
t.Properties.VariableNames = {'Person', 'Weekday', 'Location'};
%Note: the current format of your excel sheet is not practical.
%Just the data with a column header would mean that the above two lines can be replaced with:
%t = readtable('help.xls');
[groups, groupedtable] = findgroups(t(:, [1 2]));
groupedtable.Locations = splitapply(@(locs) {strjoin(locs, ',')}, t.Location, groups)
  2 Comments
Ananya Malik
Ananya Malik on 20 Mar 2017
Hi @Guillaume. Thank you for the answer. Works perfectly for given data . However I have a large data set with 227k entries and 1083 different people. 'readtable' has a limit of 65536 entries. Thank you for your patience.
Guillaume
Guillaume on 20 Mar 2017
No, readtable does not have a limit (other than the one imposed by Excel itself which is currently 1,048,576).
However, the .xls format does have a limit of 65,536 rows. This has nothing to do with readtable but is a restriction imposed by excel. The simplest way to fix this is to use the .xlsx format.

Sign in to comment.

More Answers (1)

ES
ES on 20 Mar 2017
read using
xlsread
sort column 2 using
sort

Community Treasure Hunt

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

Start Hunting!