Reorder table variables / columns

603 Ansichten (letzte 30 Tage)
Alan
Alan am 18 Sep. 2014
Kommentiert: cui,xingxing am 17 Nov. 2023
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?

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 18 Sep. 2014
Bearbeitet: Geoff Hayes am 18 Sep. 2014
Alan - you can try the following to move the 20th column to the second position
CUTable = [CUTable(:,1) CUTable(:,20) CUTable(:,2:19)];
It isn't as elegant as a simple "move column command" but it should do what you want. In the following example, I inserted the last column in before the second (note that I use end here rather than the last column number)
load patients;
T = table(Age,Diastolic,Gender,Height,LastName,Location,...
SelfAssessedHealthStatus,Smoker,Systolic,Weight)
T = [T(:,1) T(:,end) T(:,2:end-1)]
  3 Kommentare
Ole
Ole am 30 Okt. 2014
Bearbeitet: Ole am 30 Okt. 2014
You can also use the Workspace browser to modify your table by hand. The useful thing is that Matlab shows you the commands it performs for each action. For example when rearranging columns it does:
T=table; T.x=1; T.y=2; % setup demo table
T=T(:,[2 1]); % rearrange x and y columns
For the above example by Geoff Hayes that would be
T = T(:,[1 end 2:end-1])
which is apparently the way you are supposed to do it.
Peter Perkins
Peter Perkins am 21 Feb. 2018
Bearbeitet: Peter Perkins am 21 Feb. 2018
Yes, for a simple rearrangement, what Ole suggests is the best way. In some cases, it will be more readable use variable names, something such as
T = array2table(rand(10,3),'VariableNames',{'x' 'y' 'z'})
T = T(:,{'z' 'y' 'x'})
but with more than a handful of variables, probably using numbers is more readable.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Eric Sofen
Eric Sofen am 28 Mär. 2018
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:

Ben Oeveren
Ben Oeveren am 21 Feb. 2018
Bearbeitet: Ben Oeveren am 21 Feb. 2018
Or you can use:
oldvariables = T.Properties.VariableNames;
newvariables = {'date','Title','Website'};
[~,LOCB] = ismember(oldvariables,newvariables);
newTable = T(:,LOCB);
  1 Kommentar
Justin Elstrott
Justin Elstrott am 13 Jan. 2021
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);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by