Printing string as a variable
显示 更早的评论
I have a table (mytable)with 'm' rows and I am trying to add multiple empty columns with names that are in ascending numerical order (variable_01var, variable_02var etc) :
for i = 1:16
number = sprintf('%02d',i);
variable = insertAfter('mytable.variable_var', '_', number)
sprintf(variable) = zeros(m,1);
end
I am not sure how to get the generated variable name printed as a variable though?
Any help much appreciated, thanks!
Rob
3 个评论
Rik
2017-9-20
Are you using the data type table, or are you calling your matrix a table?
What you are looking for is the function eval, but DON'T DO THIS. You can see a discussion on why using eval is bad here or a more recent one here.
I would advise you to think what it is you want to do, because it sounds like the table data type has what you need. (on older releases it is not available)
rob
2017-9-20
Stephen23
2017-9-20
" I am trying to add multiple empty columns with names that are in ascending numerical order (variable_01var, variable_02var etc)"
Do NOT do this.
Read what the MATLAB documentation has to say about this: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array"
You should also read this:
and after that keep your data in one array and use indexing, or use a table.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Tables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!