how to remove last characther from cell array of string

22 ビュー (過去 30 日間)
Mederic Mainson
Mederic Mainson 2016 年 9 月 22 日
コメント済み: Walter Roberson 2016 年 9 月 22 日
Hi All, let's say i have this cell array of string:
test={'1234T'; '4567T'; '8901T'};
Is the a way to remove the last character T on every cell using this kind of command:
test=test{:}(1:end-1);
If not what is the best way to do it without loop? Cheers Medie
  2 件のコメント
Mederic Mainson
Mederic Mainson 2016 年 9 月 22 日
Hi, I think i found the best way...
test= cellfun(@(x) x(1:end-1), test, 'UniformOutput', false);
Is it the best way?
Walter Roberson
Walter Roberson 2016 年 9 月 22 日
It is a good way. Depending how large the array is, it is possible that regexprep might be faster on large matrices; I would need to benchmark to verify that though.

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

採用された回答

Walter Roberson
Walter Roberson 2016 年 9 月 22 日
I have not looked at all carefully, but if you are using R2016b then I gather the new string data type has functions that can be used for this kind of thing without using a loop.
Other than that there are multiple ways. The easiest is probably
test = cellfun(@(S) S(1:end-1), test, 'Uniform', 0);
which uses an implicit loop.
You could also use
test = regexprep(test, '.$', '', 'lineanchors');
which means "replace the character right before the end of string with nothingness"

その他の回答 (0 件)

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by