How to save row vector or cell array in a Table?

19 views (last 30 days)
I am trying to create a table that can save rowvector or cell array as each emtry in the table:
% Define the size and variable types of the table
numRows = 215;
numVars = 9;
rowvector = rand(1, 400);
rowcell = num2cell(rowvector);
tab = table('Size',[numRows,numVars],'VariableTypes',["cell","cell","cell","cell","cell","cell","cell","cell","cell"]);
for i = 1:numRows
for j = 1:numVars
tab{i, j} = rowcell; % Example initialization
end
end
I have tried both cell array and double vartypes but getting Error using {}
The value on the right-hand side of the assignment has the wrong width. The assignment requires a value whose width is 1.
How to resolve this?
  2 Comments
Image Analyst
Image Analyst on 16 Jan 2024
How many columns do you want in your array: 1 column with each row being a 1x400 cell array, OR 400 columns with each row being a 1x1 cell where the cell contains a number?
Why do you want to put the numbers into a cell array instead of just being the numbers themselves?
Subhadip
Subhadip on 16 Jan 2024
this worked for what I needed to do:
for k=1:9
Temp_Var = tab{:,k};
[Temp_Var{:}] = deal(rowvector);
tab{:,k} = Temp_Var;
end
Got the same result using arrayfun..just another way.

Sign in to comment.

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 16 Jan 2024
Edited: Sulaymon Eshkabilov on 16 Jan 2024
Here is a couple of ways to convert row vector into a table and cell array, e.g.:
numRows = 200;
numVars = 2;
rowvector = rand(1, 400);
% Table array 1 -by- 400 (1 row by 400 columns)
TAB_ARRAY1 = array2table(rowvector)
% Reshape the row vector to make numRows - by - numVars:
rowvector_new = reshape(rowvector, [numRows, numVars]);
% Table array 2 -by- 200 (2 rows -by- 200 columns)
TAB_ARRAY2 = array2table(rowvector_new)
% Cell array 1 -by- 400:
CELL_ARRAY1 = num2cell(rowvector)
% Cell array 2 -by- 200:
CELL_ARRAY2 = num2cell(rowvector_new)
  1 Comment
Sulaymon Eshkabilov
Sulaymon Eshkabilov on 16 Jan 2024
The variable names in the table array can be adjusted in a certain pattern, e.g.:
rowvector = rand(1, 400);
% Convert ARRAY-2-TABLE:
TAB_ARRAY1 = array2table(rowvector);
% Generate variable names in a certain pattern:
PAT = 'Tab_%d';
N_Vars = size(rowvector, 2);
Var_Names = cell(1, numVars);
for jj = 1:N_Vars
Var_Names{jj} = genvarname(sprintf(PAT, jj));
end
% Assign the variable names to the table columns:
TAB_ARRAY1 .Properties.VariableNames = Var_Names;

Sign in to comment.


Pratyush
Pratyush on 16 Jan 2024
Hi Subhadip,
The error you're encountering is due to trying to assign a cell array ("rowcell") directly into a single cell of the table. When you use curly braces "{}" in MATLAB, you are trying to assign to a single element of a cell array, but "rowcell" itself is a cell array with multiple elements.
You can't directly assign a cell array into a table cell using tab{i, j} = rowcell because rowcell is a cell array containing a single cell array. You should assign the inner cell array directly.
Here is the modified code:
% Define the size and variable types of the table
numRows = 215;
numVars = 9;
rowvector = rand(1, 400);
rowcell = num2cell(rowvector); % Convert the row vector to a cell array
tab = table('Size',[numRows,numVars],'VariableTypes',repmat("cell", 1, numVars));
for i = 1:numRows
for j = 1:numVars
tab{i, j} = {rowcell}; % Assign the cell array as a single entry in the table cell
end
end
In this code, rowcell is a cell array, and by using {rowcell} we are creating a single cell containing the rowcell as its content, which can be assigned to a cell in the table.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!