a simpler way to add a string variable to a table

조회 수: 21 (최근 30일)
alpedhuez
alpedhuez 2020년 12월 19일
댓글: alpedhuez 2020년 12월 19일
I have a table T
Date City Visitors
----------------------
1/1/2020 New York 10
1/2/2020 New York 20
I want to add a variable 'state' that looks like
Date City Visitors State
----------------------
1/1/2020 New York 10 NY
1/2/2020 New York 20 NY
I did
state=strings(height(T),1)
then
state(:) = 'NY'
then I create a table and merged. But is there a simpler way?
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 12월 19일
편집: KALYAN ACHARJYA 2020년 12월 19일
T.newVar(:,1)=state;
Please ensure that state have same length as T, else you may have to use repmat
alpedhuez
alpedhuez 2020년 12월 19일
But then you have to substiute a value 'state'?

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

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 12월 19일
The doc is your friend. You should really learn how to use it.
This example shows a simple way to add a new variable to a table: Calculate and Add Result as Table Variable

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by