MATLAB Answers

Multiplying a string with 1 or 0

8 ビュー (過去 30 日間)
Davor Pavlovski
Davor Pavlovski 2019 年 12 月 9 日
編集済み: the cyclist 2019 年 12 月 9 日
Check_B = 0; % checks if condition for B is satisfied
string = strcat('A', Check_B*'+B')
I want to be able to add one string to another only if a condition is satisfied, without making an if statement or switch statement.
If Check_B = 0, ('+B') will not be added, otherwise it will be.
The code provided works, however it gives a warning.
Is there a cleaner way to do the same? (I believe this is possible in Python for example)

  0 件のコメント

サインイン to comment.

採用された回答

the cyclist
the cyclist 2019 年 12 月 9 日
編集済み: the cyclist 2019 年 12 月 9 日
Here is one way:
c = ['A', repmat('+B',Check_B)];
Be aware that you are actually working with character arrays, not strings.

  1 件のコメント

Adam
Adam 2019 年 12 月 9 日
And also, that by naming your variable 'string' you are hiding the actual string functionality of Matlab by your variable, which is never a good idea.

サインイン to comment.

その他の回答 (0 件)

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


Translated by