MATLAB Answers

Is there a simpler way to create an empty Table with a list of VariableNames?

1,486 views (last 30 days)
Andrei Makarskiy
Andrei Makarskiy on 20 Sep 2015
Commented: Markus de Ruijter on 27 Mar 2020 at 13:35
I had to find a long long way around to solve this simple task.
optionsChange = cell2table(cell(0,9));
optionsChange.Properties.VariableNames{'Var1'}='Exp';
optionsChange.Properties.VariableNames{'Var2'}='Strike';
optionsChange.Properties.VariableNames{'Var3'}='Put_Mark';
optionsChange.Properties.VariableNames{'Var4'}='Put_Ask';
optionsChange.Properties.VariableNames{'Var5'}='Put_Bid';
optionsChange.Properties.VariableNames{'Var6'}='Put_Delta';
optionsChange.Properties.VariableNames{'Var7'}='Put_ImplVol';
optionsChange.Properties.VariableNames{'Var8'}='Date';
optionsChange.Properties.VariableNames{'Var9'}='DateNM';
Isn't there a nicer way to do it? Matlab really gets on my nerv after PHP and VBA

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Sep 2015
optionsChange = cell2table(cell(0,9), VariableNames, {'Exp', 'Strike', 'Put_Mark', 'Put_Ask', 'Put_Bid', 'Put_Delta', 'Put_ImplVol', 'Date', 'DateNM'});

  3 Comments

Andrei Makarskiy
Andrei Makarskiy on 21 Sep 2015
optionsChange = cell2table(cell(0,9), VariableNames, {'Exp', 'Strike', 'Put_Mark', 'Put_Ask', 'Put_Bid', 'Put_Delta', 'Put_ImplVol', 'Date', 'DateNM'});
I get this error
| Undefined function or variable 'VariableNames'.|
Walter Roberson
Walter Roberson on 21 Sep 2015
optionsChange = cell2table(cell(0,9), 'VariableNames', {'Exp', 'Strike', 'Put_Mark', 'Put_Ask', 'Put_Bid', 'Put_Delta', 'Put_ImplVol', 'Date', 'DateNM'});

Sign in to comment.

More Answers (2)

Peter Perkins
Peter Perkins on 21 Sep 2015
Andrei, Walter's solution works, as would
optionsChange = array2table(zeros(0,9), 'VariableNames',{...});
or even
optionsChange = array2table(zeros(0,9));
optionsChange.Properties.VariableNames = {...};
(Both of those have the advantage that they create 0x1 variables in the table, rather than 0x0, and so if you subsequently do something like
>> optionsChange.Exp(2) = 2
optionsChange =
Exp Strike Put_Mark Put_Ask Put_Bid Put_Delta Put_ImplVol Date DateNM
___ ______ ________ _______ _______ _________ ___________ ____ ______
0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0
everything will grow in the vertical direction.)
BUT: based on your variable names, you may not end up what you're ultimately looking for. The problem is that "create an empty table" isn't really fully specified. It's kind of like saying, "create an empty variable". The question left unanswered is, what type of variables do you want in that table? The answer might be "all doubles", but it might not. The above all create nine double variables in the table, but your last two variable names indicate dates. If you're using datenums, you're fine (datenums are are just doubles, but if you want date strings, or datetimes, you'll have to either start out with the right thing, or overwrite the doubles in the table that you want to be dates. For example,
>> vnames = {'Exp', 'Strike', 'Put_Mark', 'Put_Ask', 'Put_Bid', 'Put_Delta', 'Put_ImplVol', 'Date', 'DateNM'};
>> optionsChange = array2table(zeros(0,9), 'VariableNames',vnames);
>> optionsChange.Date = datetime(zeros(0,3)); % simple way to get a 0x1 datetime
>> optionsChange.DateNM = datetime(zeros(0,3));
>> summary(optionsChange)
Variables:
Exp: 0x1 double
Strike: 0x1 double
Put_Mark: 0x1 double
Put_Ask: 0x1 double
Put_Bid: 0x1 double
Put_Delta: 0x1 double
Put_ImplVol: 0x1 double
Date: 0x1 datetime
DateNM: 0x1 datetime
And finally, as is the case with any kind of variable in MATLAB, instead of creating an empty then growing it row by row, you might consider creating a table that's the right size but filled with NaNs and empty strings or whatever. For example,
optionsChange = array2table(nan(0,9));
That may be faster in the long run, because it doesn't have to keep reallocating memory as the table grows. That may or may not be useful or even possible in your case, just a suggestion.
Hope this helps.

  5 Comments

Show 2 older comments
Lachlan Noller
Lachlan Noller on 21 Jul 2017
Just wondering how you would round numbers in a table. I keep getting numbers like -5.2018e+05 but i want them rounded to whole integers (i.e. -520181)
Walter Roberson
Walter Roberson on 21 Jul 2017
Lachlan Noller: the easiest thing is to round the numbers before putting them in the table.
Peter Perkins
Peter Perkins on 21 Jul 2017
Also, they may already be rounded, and what you're seeing is just the shortg display. Perhaps just set your command window to use the longg format.

Sign in to comment.


Markus de Ruijter
Markus de Ruijter on 27 Mar 2020 at 7:07
Edited: Markus de Ruijter on 27 Mar 2020 at 7:34
I know this is an old question but I usually do the following to initialise a new empty table:
% Make N by 2 matrix of fieldname + value type
variable_names_types = [["id", "double"]; ...
["x", "double"]; ...
["y", "double"]; ...
["time", "double"]; ...
["type", "string"]; ...
["description", "string"]];
% Make table using fieldnames & value types from above
my_table = table('Size',[0,size(variable_names_types,1)],...
'VariableNames', variable_names_types(:,1),...
'VariableTypes', variable_names_types(:,2));
I think this is quite readable and allows you to add value types.

  2 Comments

Steven Lord
Steven Lord on 27 Mar 2020 at 13:16
FYI the functionality you're using in your answer, the ability to specify 'Size' and 'VariableTypes' in the table constructor, was introduced in release R2018a which is why it wasn't suggested previously.
Markus de Ruijter
Markus de Ruijter on 27 Mar 2020 at 13:35
Ah I see, then my answer might be useful for people using later versions of matlab. Thanks for the addition!

Sign in to comment.

Sign in to answer this question.

Tags