Delete array elements with Coder compatibility

I need to delete elements of a string with compatibility for Coder. For example:
myStr = 'This is a fun problem!'
myStr(10) = '';
Now this works just fine in Matlab, but I get an 'The right and left hand sides must have the same number of elements' error when I try to use Coder at the 'Check for Issuse' stage. I realize that I could do the following,
myStr = 'This is a fun problem!'
myStr = myStr([1:9,11:numel(myStr)]);
but this is ugly and challenging because I am doing this as part of a loop where the indexes would get complicated quickly.
Any ideas? Thanks.

 採用された回答

David Hill
David Hill 2022 年 2 月 15 日

0 投票

myStr(10)=[];

3 件のコメント

Michael
Michael 2022 年 2 月 15 日
Interesting. I originally coded this, but then changed it because I thought that Coder would see the [] input as a double and throw an error, as it would need a string. I tried this and it works though. Can you explain why this works? Why does passing [] work but passing '' does not?
David Hill
David Hill 2022 年 2 月 15 日
That syntax [] completely deletes the positions indicated. You can delete entire rows and columns also.
m=randi(100,10);
m(5,:)=[]
m(:,4)=[]
Michael
Michael 2022 年 2 月 15 日
Interesting. I thought Coder would take issue with this, as I thought [] was an empty numeric array. Thanks for the help.

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

その他の回答 (1 件)

Michael
Michael 2022 年 2 月 15 日

0 投票

Figured out a solution. Redefine the string use logical indexing to select all but the element you want to exclude.
myStr = myStr(1:numel(myStr) ~= 10)

カテゴリ

ヘルプ センター および File ExchangeMATLAB Coder についてさらに検索

製品

リリース

R2021a

質問済み:

2022 年 2 月 15 日

コメント済み:

2022 年 2 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by