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!