Using strc to make elements for calling a table column does not work

I need to call some columns of a table in automated way. The name pof table is T and I use this line to make the T.table_column:
variable_names=strcat('T.',var(i));
and the output of this code is:
variable_names =
1×1 cell array
{'T.i_nPix'}
I need to call T.i_nPix in which T is table name and i_nPix is table column. But this format {'T.i_nPix'} which is cell array and string is not able to be recognized as table.table_column. How can I solve the issue?

 采纳的回答

T.(var{i})

4 个评论

Hi Walter. Thanks.
Do you mean I should change the code lik this?
variable_names=strcat('T.(',var(i),')');
No, do not construct variable_names in that form -- there is nothing useful (and desirable) you can do with that form.
I need to call T.i_nPix in which T is table name and i_nPix is table column
You would not do that by constructing the string 'T.i_nPix' and then asking MATLAB to convert the string into a table column reference. Instead, in the place you need to use the content of the column you would code
T.(var{i})
For example you could have
T.(var{i}) = T.(var{i}) + 1;
In turn, internally this would be equivalent to
[found, idx] = ismember(var{i}, T.Properties.VariableNames);
if ~found
error('table variable "%s" not found', var{i});
end
T{:,idx} = T{:,idx} + 1;
That is, internally potential column names are looked up in the VariableNames property in order to find the column index, and that numeric index is used in the table.
"Do you mean I should change the code lik this? strcat('T.(',var(i),')')"
No, that would be entirely the wrong way to go about this. Much simpler and more efficient to follow Walter Roberson's advice... which is the same as the MATLAB documentation:

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by