MATLAB Answers

delete an element in char array without deleting the duplicated elements before it

28 ビュー (過去 30 日間)
mia saam
mia saam 2017 年 11 月 11 日
コメント済み: mia saam 2017 年 11 月 11 日
if I have a char array ex: 'drinking tea' and I padded this string with 'ab' till its size becomes 16 but now when I want to delete the padded characters the 'a' at the end also deleted how can I delete the padded elements without affecting the original string

  0 件のコメント

サインイン to comment.

回答 (2 件)

Star Strider
Star Strider 2017 年 11 月 11 日
One approach:
str = 'drinking tea';
stra = [str 'ab' 'ab'];
idx = strfind(stra, 'ab');
recovered_str = stra(1:idx(1)-1)

  5 件のコメント

表示 2 件の古いコメント
mia saam
mia saam 2017 年 11 月 11 日
this is the error message that I received
'Attempted to access idx(1); index out of bounds because numel(idx)=0.'
I'm using a larger array with a variable size and the padding will be different depending on the size of my array, the issue is that if the last char will be padded is 'a' it will not be recognized ,is there a way to delete the 'a' at the end after deleting the 'ab'? but if a string end up with 'a' it will also be deleted
Star Strider
Star Strider 2017 年 11 月 11 日
I am simply addressing the Question you posed. I cannot guarantee that my code is robust to all problems.
I will delete my Answer in a few hours.
mia saam
mia saam 2017 年 11 月 11 日
thank you for your replies, and if you like don't delete the answer, it's working fine, but not for my problem, greetings.

サインイン to comment.


Walter Roberson
Walter Roberson 2017 年 11 月 11 日
No, this is not possible to do without information about the intended size of each of the strings. As long as you pad with a pattern that is also valid potential input, you are going to run into problems if the valid input happens to end with that pattern. If you pad with 'ab' pairs then run into problems with (for example) 'nice grab' .
To get around this you either need to keep track of the length of each string, or you need to pad with something that is defined as not being valid input. For example you might be able to get away with padding with char(65535)

  1 件のコメント

mia saam
mia saam 2017 年 11 月 11 日
yes I'm having issues I think I'm going to pad with special characters like '?' or '#' it's the only way like you said padding with 'ab' will cause a lot of problems, thank you

サインイン to comment.

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

タグ


Translated by