MATLAB Answers

0

table variable name based on array

Asked by Azura Hashim on 29 Jul 2016
Latest activity Commented on by Azura Hashim on 30 Jul 2016
Hi, is it possible to assign table variable name based on strings in an array. For example:
colnames={'a' 'b'};
a=[1;2];
b=[3;4];
c=table();
c.colnames{1}=a;
c.colnames{2}=b;
Thank you.

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by James Tursa
on 29 Jul 2016
 Accepted Answer

Do you mean like this:
c.(colnames{1}) = a;
c.(colnames{2}) = b;

  1 Comment

Works perfectly thanks.

Sign in to comment.


Answer by Steven Lord
on 29 Jul 2016

Set the VariableNames of the table when you create it.
colnames = {'a' 'b'};
a = [1;2];
b = [3;4];
c = table(a, b, 'VariableNames', colnames)
Or change them later by assigning to the VariableNames property of the table.
newnames = {'Alice', 'Bob'};
c.Properties.VariableNames = newnames

  1 Comment

This works too thanks.

Sign in to comment.