MATLAB: Reorder table variables / columns

columnsMATLABtablevariables

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?

Best Answer

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)]