Define a collection of variables

1 view (last 30 days)
alpedhuez on 16 Jan 2021
Commented: alpedhuez on 17 Jan 2021
Suppose I have a table
date visitor city dummy_for_January dummy_for_February dummy_for_March
I want to define a collection of such dummy variables so that I do not need to write them one by one each time. Please advise.
Image Analyst
Image Analyst on 16 Jan 2021
I have no idea. To create a variable, you're going to have to write it - you'll have to see its name down in your script somewhere, which means you typed it. It won't just magically create itself somehow without being written. Try this:

Sign in to comment.

Accepted Answer

Matt J
Matt J on 17 Jan 2021
Since you already have the data in table form, you can just do things like,

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 16 Jan 2021
mons = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'};
vars = [string({'date', 'visitor', 'city'}), "dummy_for_" + string(mons)];
nvars = length(vars);
T = array2table(zeros(0,nvars), 'VariableNames', vars)
T = 0x15 empty table
ans = 1x15 cell array
{'date'} {'visitor'} {'city'} {'dummy_for_January'} {'dummy_for_February'} {'dummy_for_March'} {'dummy_for_April'} {'dummy_for_May'} {'dummy_for_June'} {'dummy_for_July'} {'dummy_for_August'} {'dummy_for_September'} {'dummy_for_October'} {'dummy_for_November'} {'dummy_for_December'}
alpedhuez on 17 Jan 2021
I simply like to define a new variable that is a set of dummy variables in the above example. Is it possible?

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!