Clear Filters
Clear Filters

Assign an empty table

836 views (last 30 days)
Birsen
Birsen on 25 Apr 2017
Commented: Amos on 29 Jun 2023
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

Accepted Answer

Birsen
Birsen on 26 Apr 2017
Edited: Walter Roberson on 26 Apr 2017
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 Comments
Walter Roberson
Walter Roberson on 8 Oct 2019
>> 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 on 29 Jun 2023
... 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'} ...
)

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 25 Apr 2017
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 Comments
Birsen
Birsen on 25 Apr 2017
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 on 26 Apr 2017
>> 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

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!