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

49 ビュー (過去 30 日間)
Subhadip
Subhadip 2024 年 1 月 16 日
回答済み: Pratyush 2024 年 1 月 16 日
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 件のコメント
Image Analyst
Image Analyst 2024 年 1 月 16 日
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 2024 年 1 月 16 日
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.

サインインしてコメントする。

回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2024 年 1 月 16 日
編集済み: Sulaymon Eshkabilov 2024 年 1 月 16 日
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 件のコメント
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2024 年 1 月 16 日
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;

サインインしてコメントする。


Pratyush
Pratyush 2024 年 1 月 16 日
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.

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by