how to sort a cell array inside a struct?

3 views (last 30 days)
Chiara Scarpellini
Chiara Scarpellini on 29 Jul 2021
Answered: Peter Perkins on 29 Jul 2021
我有这个结构,我必须每个vector on the right side
T=table(string,ID_number);
[G, IDs] = findgroups(T.string);
C = splitapply(@(x){x},T.ID_number,G);
Name_Code = struct('name', cellstr(IDs),'Code', C);

Accepted Answer

Peter Perkins
Peter Perkins on 29 Jul 2021
I think you would be better off with a table than a struct.
Name_Code = table(IDs, C,'VariableNames',["Name" "Codes"])
Name_Code.Codes = cellfun(@sort,Name_Code.Code,"UniformOutput",false)
But Jan is correct, do it at the source.

More Answers (1)

Jan
Jan on 29 Jul 2021
Expand
C = splitapply(@(x) {x}, T.ID_number, G);
to
C = splitapply(@(x) {sort(x)}, T.ID_number, G);

Community Treasure Hunt

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

Start Hunting!