필터 지우기
필터 지우기

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

조회 수: 23 (최근 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 CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by