Clear Filters
Clear Filters

add column in table

92 views (last 30 days)
Luca Re
Luca Re on 11 Jul 2023
Commented: Luca Re on 12 Jul 2023
T= struct2table(G);
c=1:length(T);
c=c';
i want to add c as new column of T!

Accepted Answer

Voss
Voss on 11 Jul 2023
G = struct('ones',{1 1 1 1},'twos',{2 2 2 2}); % for example
T= struct2table(G);
c=1:height(T); % use height
c=c';
T.new_column = c
T = 4×3 table
ones twos new_column ____ ____ __________ 1 2 1 1 2 2 1 2 3 1 2 4
  7 Comments
Voss
Voss on 12 Jul 2023
[Is it] possible [to] move c in the first column?
Yes, see below:
G = struct('ones',{1 1 1 1},'twos',{2 2 2 2}); % for example
T= struct2table(G);
c=1:height(T); % use height
c=c';
T.new_column = c;
T = T(:,[end 1:end-1])
T = 4×3 table
new_column ones twos __________ ____ ____ 1 1 2 2 1 2 3 1 2 4 1 2
Luca Re
Luca Re on 12 Jul 2023
thank

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!