How do I assign a value to a particular column in a subsection of a table?

58 ビュー (過去 30 日間)
Karen
Karen 2017 年 5 月 9 日
コメント済み: Peter Perkins 2017 年 5 月 10 日
Using the example testing data on https://www.mathworks.com/help/matlab/matlab_prog/calculations-on-tables.html, I would like to write something like
T(T.Gender=='female',:).Test2 = 100
I can do something similar with array data, but get an error with table data. Is there a simple way to make this work without having to create a dummy array of the appropriate length for my constant value?
T = readtable(fullfile(matlabroot,'examples','matlab','testScores.csv'),...
'ReadRowNames',true)
T.Gender = categorical(T.Gender)
% select a subset of the rows
rows = T.Gender=='female'
% this works, and is my usual way of working with array data
A = table2array(T(:,2:4))
A(rows,2) = 100
% This works, but is ugly
T(rows,:).Test2 = 100 * ones(size(T(rows,:).Test2))
% This does not work
T(rows,:).Test2 = 100
Error: 'To assign to or create a variable in a table, the number of rows must match the height of the table. '

採用された回答

Walter Roberson
Walter Roberson 2017 年 5 月 9 日
T{rows,'Test2'} = 100;
  2 件のコメント
Karen
Karen 2017 年 5 月 9 日
Thanks. After posting the question, I finally found the right page of documentation...
Peter Perkins
Peter Perkins 2017 年 5 月 10 日
For assigning to just one variable in a table, you might find
T.Test2(rows) = 100;
more readable.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by