a simpler way to add a string variable to a table

19 Ansichten (letzte 30 Tage)
alpedhuez
alpedhuez am 19 Dez. 2020
Kommentiert: alpedhuez am 19 Dez. 2020
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 Kommentare
KALYAN ACHARJYA
KALYAN ACHARJYA am 19 Dez. 2020
Bearbeitet: KALYAN ACHARJYA am 19 Dez. 2020
T.newVar(:,1)=state;
Please ensure that state have same length as T, else you may have to use repmat
alpedhuez
alpedhuez am 19 Dez. 2020
But then you have to substiute a value 'state'?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 19 Dez. 2020
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

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by