Assign an empty table

조회 수: 838 (최근 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