How to replace a string with another string in specific table columns

14 次查看(过去 30 天)
Hello,
Im trying to replace all [] with '-99' only in the columns that begin with Q_ in the table t below. How would I do that with an index ? Its important that I dont use the variables names.
% data
Q_x = {'1'; '[]'; '3'};
a = {'A'; 'B'; 'C'};
o = {'A'; '[]'; 'C'};
Q_y = {'1'; '[]'; '3'};
t = table(Q_x, a, o, Q_y)
all_vars = t.Properties.VariableNames;
index = find(strncmp(all_vars, 'Q_', 2));
Thank you,

采纳的回答

Daniel M
Daniel M 2019-10-28
t{:,index} = regexprep(t{:,index},'\[\]','-99')

更多回答(1 个)

Guillaume
Guillaume 2019-10-28
编辑:Guillaume 2019-10-28
One easy way:
missinglocs = ismissing(t, '[]');
newt = fillmissing(t, 'constant', '-99', 'DataVariables', startsWith(t.Properties.VariableNames, 'Q_'), 'MissingLocations', missinglocs)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by