adding variablenames to multiple columns in cell2table

18 visualizaciones (últimos 30 días)
BobbyRoberts
BobbyRoberts el 4 de Jul. de 2020
Comentada: Image Analyst el 5 de Jul. de 2020
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!

Respuestas (1)

Image Analyst
Image Analyst el 4 de Jul. de 2020
Editada: Image Analyst el 4 de Jul. de 2020
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 comentarios
BobbyRoberts
BobbyRoberts el 5 de Jul. de 2020
also in this code you have two columns for each variable..
Image Analyst
Image Analyst el 5 de Jul. de 2020
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by