confusing error while using Table
14 次查看(过去 30 天)
显示 更早的评论
I am trying to make a simple table so that I can access some data by using names instead of a numeric index of a cell.
locationsTable = table(splitLocations, 'VariableNames', headerNames);
headerNames = {'rawData', 'testConfig', 'efiData', 'bonusData'};
splitLocations = {2, 49, 74, 142};
However, when I try to run this, I get the error
Error using setVarNames (line 35) The VariableNames property must contain one name for each variable in the table.
Error in table (line 304) t = setVarNames(t,vnames); % error if invalid, duplicate, or empty
This is confusing me, since I have a name for each variable, and none of them are invalid, duplicate or empty. I am unsure what I am doing wrong and what I need to change so that it works. Any help would be appreciated.
0 个评论
采纳的回答
Sean de Wolski
2015-8-4
locationsTable = table(splitLocations{:}, 'VariableNames', headerNames);
It's seeing splitLocations as one variable, not four. Two have it see each scalar as a separate value, use comma-separated list expansion to pass in all four inputs as scalars.
1 个评论
Peter Perkins
2015-8-5
Or use cell2table:
>> cell2table(splitLocations,'VariableNames',headerNames)
ans =
rawData testConfig efiData bonusData
_______ __________ _______ _________
2 49 74 142
A cell array is (in this context) just like any other MATLAB variable: if you pass one variable into table, you get a table with one variable (although in your case of course you got an error because you had four names). And what's more, because splitLocations was 1x4, you would have gotten a table containing one variable that itself had four columns. In other words, this
>> table(splitLocations)
ans =
splitLocations
______________
[1x4 cell]
is perfectly legal, it just probably isn't what you wanted.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Preprocessing Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!