How do I instert a string in all rows of a table columnI

28 views (last 30 days)
I am trying to do something that I would think is pretty simple: insert the same string in a new Variable of an existing table with, say 100 rows:
T.NewColumn = 'SomeText';
I keep getting this error: To assign to or create a variable in a table, the number of rows must match the height of the table.
Whatam I dowing wrong?

Answers (1)

Peter Perkins
Peter Perkins on 16 Nov 2017
You are trying to assign a variable with one row to a table with 100 rows. Scalar expansion doesn't apply here, because you have no row subscript in that assignment. You want either
T.NewColumn(:) = 'SomeText';
or
T{:,'NewColumn'} = 'SomeText';
But hang on - actually that won't work: you are also trying to create a char matrix. Probably not a great idea, for a variety of reasons. In recent MATLAB, change those single quotes to double quotes and use strings. Prior to R2016b, you likely want to assign {'SomeText'}.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!