Effacer les filtres
Effacer les filtres

Assign an empty table

836 vues (au cours des 30 derniers jours)
Birsen
Birsen le 25 Avr 2017
Commenté : Amos le 29 Juin 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

Réponse acceptée

Birsen
Birsen le 26 Avr 2017
Modifié(e) : Walter Roberson le 26 Avr 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 commentaires
Walter Roberson
Walter Roberson le 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 le 29 Juin 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'} ...
)

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 25 Avr 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 commentaires
Birsen
Birsen le 25 Avr 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 le 26 Avr 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by