adding variablenames to multiple columns in cell2table
24 次查看(过去 30 天)
显示 更早的评论
Hi I have a 30 x 42 cell array. I want to turn this into a table and add variable names for every 7 columns such that I will have 6 variable names. I just don't know how to add the variable names to multiple columns. How I would normally do it is below (say if there were only 7 columns).
names = {'first','second','third','fourth','fifth','sixth','seventh'};
tbl = cell2table(curmat, 'VariableNames',names)
Thanks!
0 个评论
回答(1 个)
Image Analyst
2020-7-4
编辑:Image Analyst
2020-7-4
It is not possible. You'll have to leave it as a cell array. For a table, all columns must have a unique variable name. With a cell array, you can have anything in any cell, so you could put those into the first row:
% Make sample cell array
ca = cell(30, 42);
for k = 1 : numel(ca)
ca{k} = randi(9, 1, 2);
end
% Set up the first batch of variable names.
variableNames = {'first','second','third','fourth','fifth','sixth','seventh'};
% Find out how many copies of this we can replicate to fill up the top row.
numCopies = int32(size(ca, 2) / length(variableNames)) % This is 6 for this particular case.
% Now replicate variable names across the cell array.
variableNames = repmat(variableNames, [1, numCopies]); % This is 42 long for this particular case.
% Now stuff variableNames into the first row of the cell array.
ca(1, 1:length(variableNames)) = variableNames
% tbl = cell2table(ca, 'VariableNames', variableNames) % NOT POSSIBLE!
However, if you're willing to have unique variable names, like with a "set number" appended to those words you have, then you could do it like this:
% Make sample cell array
ca = cell(30, 42);
for k = 1 : numel(ca)
ca{k} = randi(9, 1, 2);
end
% Replicate variable names across the cell array.
theNumbers = {'first','second','third','fourth','fifth','sixth','seventh'};
setNumber = 1;
for k = 1 : size(ca, 2)
setNumber = floor((k-1) / 7) + 1;
n = mod(k-1, 7) + 1;
variableNames{k} = sprintf('%s_%d', theNumbers{n}, setNumber);
fprintf('Variable name for column %d is %s\n', k, variableNames{k});
end
% Now make these the variable names.
tbl = cell2table(ca, 'VariableNames', variableNames)
3 个评论
Image Analyst
2020-7-5
That was just an example. I put it in there because most people don't know that you can have a single column of a table be 2 columns (kind of) if the value is a 2x1 vector. But obviously you would not be using my sample cell array "ca", right? Of course you have your own cell array with a different name. You were supposed to realize that since you didn't supply us with curmat, that I had to create my own cell array and that you would replace mine with yours.
Again, you cannot have two different table columns with the same name. If you want to do that, you'd have to have the column with each element being a 6-by-1 vector. Or else leave it as a cell array and just use the top cell in each column to hold whatever string (name) you want.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!