movevars
Moves table variables to a new position.
Category: Language Fundamentals > Data Types
Syntax
T2 = movevars(T, vars, 'Before', ref)
Places the variables vars just before the variable ref.
T2 = movevars(T, vars, 'After', ref)
Places them just after ref.
Description
Only the order of the columns changes; no data are modified. When several variables are moved, they keep their relative order. It is a neat way to bring an identifier column to the front before displaying or exporting a table.
Input arguments
T- A table or timetable.
vars- Variables to move: a name, cell array of names, positions or a logical mask.
ref- Reference variable, by name or position.
Output arguments
T2- A table with the same data and a new variable order.
Example
Reordering columns
T = table([1;2], [3;4], [5;6], 'VariableNames', {'x','y','z'});
movevars(T, 'z', 'Before', 'x')
movevars(T, 'x', 'After', 'z')
Output
ans = 2×3 table
z x y
_ _ _
5 1 3
6 2 4
ans = 2×3 table
y z x
_ _ _
3 5 1
4 6 2