Is there a simpler way to create an empty Table with a list of VariableNames?
조회 수: 621 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2015년 9월 20일
편집: Walter Roberson
2021년 6월 3일
optionsChange = cell2table(cell(0,9), 'VariableNames', {'Exp', 'Strike', 'Put_Mark', 'Put_Ask', 'Put_Bid', 'Put_Delta', 'Put_ImplVol', 'Date', 'DateNM'});
댓글 수: 4
Gabor
2021년 2월 25일
Sorry guys, but creating a table in matlab should be lot more simple.
First off why cant we create an empty table with column names?
추가 답변 (4개)
Peter Perkins
2015년 9월 21일
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
Walter Roberson
2017년 7월 21일
Lachlan Noller: the easiest thing is to round the numbers before putting them in the table.
Peter Perkins
2017년 7월 21일
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.
Markus de Ruijter
2020년 3월 27일
편집: Markus de Ruijter
2020년 3월 27일
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.
댓글 수: 4
Paolo Mazzoleni
2022년 10월 12일
this is useful for what I'm doing, but what should I use as variable type if I want let's say "x" to be an array of 2 double?
thanks
Walter Roberson
2022년 10월 12일
When individual entries are non-scalar, you have to use "cell" for the type, with the exception of "char".
Ilya Gurin
2021년 5월 20일
Good solutions, everyone, but why isn't this functionality available through table? Usually, the data type conversion functions are used when you have already created data as one type and want to convert it to another. I don't see why this:
array2table(zeros(0,9), 'VariableNames',{...});
can't look like this:
table('VariableNames',{...});
댓글 수: 10
Walter Roberson
2021년 6월 3일
I don't accept the point about a "sanity check."
Then you should be using a different programming language. MATLAB is full of sanity checks. MATLAB is specifically designed for people who are not expert programmers: people who cannot be counted upon to only work with values that are "sane" from the point of view of the programming language. People who do not use good programming practices themselves.
In context, putting in sanity checks to detect problems early is good programming practice, for what MATLAB is intended for. MATLAB's goal of efficiency is secondary to MATLAB's goal to make the software accessible to users who are to be expected to make a lot of mistakes.
Ilya Gurin
2021년 6월 3일
1) I'm all for certain kinds of sanity checks. I've spent hours fixing Code Analyzer ("m-lint") warnings, in my own code and especially other people's code. I just don't think the particular feature of requiring a Size argument when creating a table is a useful sanity check. And, in fact, your own code sample defeats such sanity-checking as may exist.
2) If the goal is to make software accessible to non-experts who make a lot of mistakes, I posit that requiring a Size parameter and a list of VariableNames that can get out of sync is contrary to that goal.
Markus Leuthold
2023년 4월 25일
Mathworks, please allow
tbl = table("VariableNames", ["var1" "var2"....]);
and avoid ugly hacks like array2table(zeros(0... or cell2table(cell(0,....)
댓글 수: 2
Steven Lord
2023년 4월 25일
Please submit this enhancement request to Technical Support directly using the Contact Support link under the Get Support heading at the end of this page.
Walter Roberson
2023년 4월 25일
As discussed in https://www.mathworks.com/matlabcentral/answers/244084-is-there-a-simpler-way-to-create-an-empty-table-with-a-list-of-variablenames#answer_422250 you can already table() indicating varible names, table size, and variable types, without needing to convert zeros() or cell()
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series Events에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!