MATLAB: Existing table, populate a column with string (receiving errors)

stringtable

I have a table, and I want to populate every row in a specific column with the same string. I keep getting errors. Using Matlab R2018a, no fancy packages.
mytable = nan(5,2);
% I'm creating my table like this; it's actually much bigger, and most of the columns are numerical
% is there a better way to do this? the order of the column matters, and I don't want to have to add extra code to re-order
mytable = array2table(mytable);
mytable.Properties.VariableNames = {'colA', 'colB'};
mytable.colA(:) = 459; % just a random number, proof-of-concept that it works
mytable.colB(:) = {'words'}; % this throws errors

Best Answer

You are close, but you are trying to assign a character vector to a double variable (NaN = not a number). You can see that if you run the command summary(mytable).
You also do not need to put the text into a cell if you use the string data type.
See if you can get this code to work for your needs.
myArray = nan(5,2)
mytable = array2table(myArray);
mytable.Properties.VariableNames = {'colA', 'colB'};
mytable2 = convertvars(mytable,"colB",'string');
mytable2.colA(:) = 459;
mytable2.colB(:) = "words"
You say you have a lot of data. If the variables already exist, you could do something like this:
colA = nan(5,1);
colB = strings(5,1);
mytable = table(colA,colB);
mytable.colA(:) = 459; % just a random number, proof-of-concept that it works
mytable.colB(:) = "words" % this throws errors