How to replace strings in a cell array

Hello,
I an wondering how to replace strings in a cell array based on the content of the previous cell. Every time that a cell in 'input' starts with the characters QQQQ, that cell is renamed with Q_ and the content of the previous cell. For example QQQQ_01 would be renamed Q_D and QQQQ_02 would be renamed Q_E and so on. How would I do that ? The input and the desired output are illustrated below.
input = {'A', 'B', 'C', 'D', 'QQQQ_01', 'E', 'QQQQ_02'}
output = {'A', 'B', 'C', 'D', 'Q_D', 'E', 'Q_E'}

1 件のコメント

Adam
Adam 2019 年 10 月 25 日
編集済み: Adam 2019 年 10 月 25 日
Seems like the kind of task so specific a for loop would do the job fine.
Note that they are chars rather than strings though, a distinction which didn't used to matter in Matlab but does now that strings have been introduced as their own data type.

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

 採用された回答

Stephen23
Stephen23 2019 年 10 月 25 日
編集済み: Stephen23 2019 年 10 月 25 日

0 投票

>> C = {'A', 'B', 'C', 'D', 'QQQQ_01', 'E', 'QQQQ_02'}
>> X = find(strncmp(C,'QQQQ',4));
>> C(X) = strcat('Q_',C(X-1))
C =
'A' 'B' 'C' 'D' 'Q_D' 'E' 'Q_E'
You will have to decide how to handle if the first cell contains 'QQQQ'.

1 件のコメント

012786534
012786534 2019 年 10 月 25 日
Thank you Stephen.

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

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

2019 年 10 月 25 日

コメント済み:

2019 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by