Assign a table coloumn a particular data type

조회 수: 2 (최근 30일)
Andrea Sbaragli
Andrea Sbaragli 2021년 5월 24일
댓글: Steven Lord 2021년 5월 24일
For other reasons I cannot initialize my table as A=table but I must do A= zeros(height(B), 8) and then through array2table I convert my array into a table.
At this point how could I set A(:,2) and A(:,6) to datetime null values?

채택된 답변

Benjamin Kraus
Benjamin Kraus 2021년 5월 24일
편집: Benjamin Kraus 2021년 5월 24일
You cannot mix data types within a double matrix, but you can mix data types within a table.
So, you cannot convert two columns of A into datetime before creating the table.
I'm curious why you cannot create your table empty and then add colums as needed.
For example:
tbl = table;
B = zeros(10,1);
tbl.Var1 = B;
tbl.Var2 = NaT(size(B))
tbl = 10×2 table
Var1 Var2 ____ ____ 0 NaT 0 NaT 0 NaT 0 NaT 0 NaT 0 NaT 0 NaT 0 NaT 0 NaT 0 NaT
However, if that doesn't work for some reason, maybe this is what you are looking for?
B = zeros(10,1);
A = zeros(height(B),8);
tbl = array2table(A);
tbl.A2 = NaT(height(A),1);
tbl.A6 = NaT(height(A),1)
tbl = 10×8 table
A1 A2 A3 A4 A5 A6 A7 A8 __ ___ __ __ __ ___ __ __ 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0
  댓글 수: 2
Andrea Sbaragli
Andrea Sbaragli 2021년 5월 24일
Yes it works perfect thank you so much
Steven Lord
Steven Lord 2021년 5월 24일
In recent releases:
types = repmat("double", 1, 8);
types([2 6]) = "datetime";
tbl = table('size', [10 8], 'VariableNames', "A"+(1:8), 'VariableTypes', types)
tbl = 10×8 table
A1 A2 A3 A4 A5 A6 A7 A8 __ ___ __ __ __ ___ __ __ 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0 0 NaT 0 0

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by