How to reorder a matlab table based on a portion of variables name
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Robert
      
 el 22 de Mayo de 2018
  
    
    
    
    
    Comentada: Robert
      
 el 23 de Mayo de 2018
            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!
0 comentarios
Respuesta aceptada
  Sean de Wolski
      
      
 el 22 de Mayo de 2018
        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:
Más respuestas (1)
Ver también
Categorías
				Más información sobre Tables en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


