Reorder table variables / columns
이전 댓글 표시
I've created a table from a large csv file using the readtable function. I want to reorder the variables / columns in this table.
For example.
My table's name is CUTable
It has a variable CUTable.Date which is the date the data was taken It is currently column 20 in the table. I want to make it column 2 and shift existing columns 2-19 to 3-20.
It seems there should be a very easy command to do this which I'm missing, but I can't seem to find it anywhere in the help or these forums. I could, but don't want to "brute force" it (e.g. create a new table, and one-by-one copy the variables from the exiting table to the new table in the order I want, then delete the old table) if avoidable.
How can I accomplish this?
채택된 답변
추가 답변 (2개)
Eric Sofen
2018년 3월 28일
9 개 추천
In R2018a, check out the new movevars function for rearranging variables in tables. It's part of a set of new functions for manipulating table variables:
댓글 수: 1
xingxingcui
2023년 11월 17일
add topic link:
Ben Oeveren
2018년 2월 21일
편집: Ben Oeveren
2018년 2월 21일
Or you can use:
oldvariables = T.Properties.VariableNames;
newvariables = {'date','Title','Website'};
[~,LOCB] = ismember(oldvariables,newvariables);
newTable = T(:,LOCB);
댓글 수: 1
Justin Elstrott
2021년 1월 13일
Hi Ben,
I like this solution, but there's a typo in the third line. I believe it should read as:
oldvariables = T.Properties.VariableNames;
newvariables = {'date','Title','Website'};
[~,LOCB] = ismember(newvariables,oldvariables);
newTable = T(:,LOCB);
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!