Assign an empty table

886 次查看(过去 30 天)
Birsen
Birsen 2017-4-25
评论: Amos 2023-6-29
Hi,
I am trying to assign an empty table (lets say "groupData" to be used later in the for loop. In the loop I use another table and fill the groupData. Later on I would like to empty the table (groupData) and refill again and so. I tried the following code , did not work.
I got the error o convert a table to numeric, use the TABLE2ARRAY function. Is there a way to assign an empty table without using table2array, because I use the groupData data after the for loop to get the data with their variable names. Like
if (groupData.Node(i)==4000)&& (groupData.Cell(i+1)==11)
Thank you
Birsen
Here is the code
==============
for h=1:3
k=newK;
m=1;
groupData(:,:)=0
while pData.Node(k)~=4000
groupData(m,:)=pData(k,:)
k=k+1;
m=m+1;
end
newK=k+1;
end

采纳的回答

Birsen
Birsen 2017-4-26
编辑:Walter Roberson 2017-4-26
I think I resolved it as follows:
groupData = array2table(zeros(0,9));
groupData.Properties.VariableNames = {'Node','x2', ...
'x3','Cell', 'x5','x6','x7', 'x8', 'x9'}
Just defining groupData = array2table(zeros(0,9)) was not sufficient. I had to also use
groupData.Properties.VariableNames = {'Node','x2', ...
'x3','Cell', 'x5','x6','x7', 'x8', 'x9'}
  3 个评论
Walter Roberson
Walter Roberson 2019-10-8
>> groupData = array2table(zeros(0,9))
groupData =
0x9 empty table
MATLAB believes that it is an empty table.
I think you failed to note that the first parameter to zeros is 0, which causes a variable with no rows to be created. Remember that a variable is considered empty if any of its dimensions is 0.
Amos
Amos 2023-6-29
... as a single statement + alternative zero-height array syntax
groupData = array2table( double.empty(0,9) ...
, 'VariableNames' ...
, {'Node','x2','x3','Cell', 'x5','x6','x7', 'x8', 'x9'} ...
)

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2017-4-25
If the idea is that you want to write 0 over top of all of the existing entries, then
groupData{:,:} = 0;
If you want to remove all of the entries, then
groupData(:,:) = [];
  2 个评论
Birsen
Birsen 2017-4-25
Walter thank you for the answer. But both gave an error I tried groupData{:,:} , got the following error "The following error occurred converting from table to cell: Conversion to cell from table is not possible".
And tried groupData(:,:) = []; got error too. "Deletion requires an existing variable".
Walter Roberson
Walter Roberson 2017-4-26
>> groupData = array2table(rand(5,2))
groupData =
5×2 table
Var1 Var2
_________________ _________________
0.929021451353742 0.945085903751417
0.723183836546625 0.705712522061558
0.346528149686704 0.801252732520179
0.453861726186205 0.680579929444058
0.993489506713988 0.784894685163496
>> groupData{:,:} = 0;
>> groupData
groupData =
5×2 table
Var1 Var2
____ ____
0 0
0 0
0 0
0 0
0 0

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by