How to access/change a specific column in table in cell array using cellfun?

Hello all.
I have a cell array A that is compsed of two tables T1, T2 with identical columns. I am struggling to find a proper method to make some changes to a specific column in these tables. I can do this using a loop, but I prefer to use cellfun to do my job for fewer coding lines. Please follow this example:
T1 = table([1;2;3],['a';'b';'c']);
T2 = table([4;5;6],['x';'y';'z']);
A = {T1,T2};
How can I access the data in column 1 of each table (Var1) to do some mathematical operation on it, like addition or subtraction. I need to know how to properly write this using cellfun. Any help will be appreciated.
Thanks in advance,
Ahmad Gad

 採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 11 日
cellfun(@(T) sum(T.var1), A)

4 件のコメント

(@(T) sum(T.Var1), A)
Ahmad Gad
Ahmad Gad 2021 年 2 月 11 日
編集済み: Ahmad Gad 2021 年 2 月 13 日
Thanks for the reply. That is not what I meant. I am sorry if my question didn't emphasize that.
I need to update the values of columns Var1 in each table. Not to create a new variable. Assume I need to add 1 to each element of the column and update its values (not to create another variable).
Thanks a lot!
cellfun(@(T) [table(T.Var1+1,'VariableNames','Var1'), T(:,2:end)], A, 'uniform', 0)
In the special case that the variable name really is Var1 you could abbreviate to
cellfun(@(T) [table(T.Var1+1), T(:,2:end)], A, 'uniform', 0)
as Var1 is the default variable name
Ahmad Gad
Ahmad Gad 2021 年 2 月 14 日
Thanks a lot Walter.
That is what I was looking for.
Best, Ahmad

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Cell Arrays についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by