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

조회 수: 2 (최근 30일)
012786534
012786534 2019년 10월 28일
편집: Guillaume 2019년 10월 28일
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