How to reorder a matlab table based on a portion of variables name

조회 수: 3 (최근 30일)
Robert
Robert 2018년 5월 22일
댓글: Robert 2018년 5월 23일
Hi,
Wondering if anyone knows how to reorder a Matlab table based on a portion of variables name (numerical ID within the variables name) when having a table with hundreds of columns (variables)
Example of table variables name:
America_1010 America_1008 America_1001 America_1808 ...
After reorder:
America_1001 America_1008 America_1010 America_1808 ...
Thank you in advance!

채택된 답변

Sean de Wolski
Sean de Wolski 2018년 5월 22일
Doing it manually,
T = array2table(1:4,'VariableNames', {'America_1010', 'America_1008', 'America_1001', 'America_1808'}) % example table
numeric_string = regexp(string(T.Properties.VariableNames), '(\d*)', 'tokens'); % grab numeric part
numeric_value = cellfun(@(x)double(x{1}), numeric_string); % convert to double
[~, idx] = sort(numeric_value); % sort
T2 = T(:,idx) % reorder
But I'd recommend Stephen's natural sorting utilities as a way to skip sorting yourself:
  댓글 수: 1
Robert
Robert 2018년 5월 23일
Thank you so much to both of you for your suggestions and links. Your solutions work great!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 5월 22일

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by