How can you create an empty table object with named columns?

조회 수: 124 (최근 30일)
Ivan
Ivan 2014년 5월 1일
편집: Stephen23 2021년 1월 7일
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?

답변 (1개)

Nikita Agrawal
Nikita Agrawal 2021년 1월 7일
  댓글 수: 3
Nikita Agrawal
Nikita Agrawal 2021년 1월 7일
You can keep data empty. Here you go:
Headers = {'A','B'}
Empty_table = cell2table(cell(1,2),'VariableNames'= Headers)
Stephen23
Stephen23 2021년 1월 7일
편집: Stephen23 2021년 1월 7일
@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'}
Headers = 1x2 cell array
{'A'} {'B'}
Empty_table = cell2table(cell(0,2),'VariableNames', Headers)
Empty_table = 0x2 empty table
Empty_table.Properties.VariableNames
ans = 1x2 cell array
{'A'} {'B'}

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by