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

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)

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

Tags

Asked:

on 2 Nov 2017

Answered:

on 16 Nov 2017

Community Treasure Hunt

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

Start Hunting!