How can you create an empty table object with named columns?
Afficher commentaires plus anciens
How can you create an empty table object with named columns?
I have a loop that logically does something like this:
a = 1
b = 2
t = [t; table(a, b)]
a = 5
b = 6
t = [t; table(a, b)]
However, I want to add an assert after each row creation, like this:
a = 1
b = 2
t = [t; table(a, b)]
assert(0 == sum(t{:,{'a'}} == t{:,{'b'}}))
a = 5
b = 6
t = [t; table(a, b)]
assert(0 == sum(t{:,{'a'}} == t{:,{'b'}}))
But since t starts off empty, before I run t{:,{'a'}} I have to check if t is empty. I imagine if I can have named columns in an empty table, the problem can be solved?
Réponses (1)
Nikita Agrawal
le 7 Jan 2021
0 votes
3 commentaires
Stephen23
le 7 Jan 2021
In that thread, the table is not empty.
Nikita Agrawal
le 7 Jan 2021
You can keep data empty. Here you go:
Headers = {'A','B'}
Empty_table = cell2table(cell(1,2),'VariableNames'= Headers)
@Nikita Agrawal: sure. But the original question asked about an empty table, not empty data.
To make an empty table, the input cell array must be empty too:
Headers = {'A','B'}
Empty_table = cell2table(cell(0,2),'VariableNames', Headers)
Empty_table.Properties.VariableNames
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!