MATLAB Answers

how to sort a cell array inside a struct?

조회 수: 2(최근 30일)
Chiara Scarpellini
Chiara Scarpellini 2021년 7월 29일
답변: Peter Perkins 2021년 7월 29일
I have this struct and I have to sort every vector on the right side
[G, IDs] = findgroups(T.string);
C = splitapply(@(x){x},T.ID_number,G);
Name_Code = struct('name', cellstr(IDs), 'Code', C);

채택된 답변

Peter Perkins
Peter Perkins 2021년 7월 29일
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.

추가 답변(1개)

Jan 2021년 7월 29일
C = splitapply(@(x) {x}, T.ID_number, G);
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!

Translated by