MATLAB Answers

How to remove a pattern in a table column

25 ビュー (過去 30 日間)
JFz
JFz 2016 年 10 月 4 日
Commented: JFz 2016 年 10 月 4 日
Hi,
I have a table with a column. The column looks like this: 'AB_Off' 'ABC' 'CDE_Off'
I would like to remove the '_Off' from every cell in that column. I tried TF = endsWith, and match, but I always get error. "Error using string string is obsolete and will be discontinued. Use char instead." I don't know how to resolve this. Would you please help? Thanks,
Jennifer

  0 件のコメント

サインイン to comment.

採用された回答

Jan Orwat
Jan Orwat 2016 年 10 月 4 日
編集済み: Jan Orwat 2016 年 10 月 4 日
assuming T - your table, and n - number of column containing these strings you can do as follows:
T{:, n} = regexprep(T{:, n}, '_Off$', '');
or
T{:, n} = erase(T{:, n}, '_Off');
but second option doesn't detect if it's on the end of a string

  1 件のコメント

JFz
JFz 2016 年 10 月 4 日
Thank you!

サインイン to comment.

More Answers (2)

Hamid Ebrahimi Orimi
Hamid Ebrahimi Orimi 2016 年 10 月 4 日
you mean you have a cell which has 'ABC_Off' string in each element? If I understood correctly, the answer is as follow: A={'ABC_Off', 'ABC_Off','ABC_Off'} for i=1:length(A) if A{i}=='ABC_Off' A{i}='ABC'; end end A is your defined cell.

  0 件のコメント

サインイン to comment.


elias GR
elias GR 2016 年 10 月 4 日
編集済み: elias GR 2016 年 10 月 4 日
strfind function is what you need ( https://www.mathworks.com/help/matlab/ref/strfind.html ). I suppose that you have a cell array of string. Then a simple general solution to remove the _Off from the end of the strings (we suppose that _Off only occurs at the end) is:
str={'AB_Off','ABC','CDE_Off'};
for i=1:length(str)
ind=strfind(str{i},'_Off');
if ~isempty(ind)
str{i}=str{i}(1:ind-1);
end
end

  1 件のコメント

JFz
JFz 2016 年 10 月 4 日
Thanks a lot.
What I have is a column in a Matlab table. the 'AB_Off', 'ABC', 'CDE_Off' are on different rows in that same column. Is there a way to make all the '_Off' in that column go away? Thanks,
Jennifer

サインイン to comment.

サインイン してこの質問に回答します。

タグ

製品


Translated by