Multiplying a string with 1 or 0

13 vues (au cours des 30 derniers jours)
Davor Pavlovski
Davor Pavlovski le 9 Déc 2019
Modifié(e) : the cyclist le 9 Déc 2019
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)

Réponse acceptée

the cyclist
the cyclist le 9 Déc 2019
Modifié(e) : the cyclist le 9 Déc 2019
Here is one way:
c = ['A', repmat('+B',Check_B)];
Be aware that you are actually working with character arrays, not strings.
  1 commentaire
Adam
Adam le 9 Déc 2019
Modifié(e) : Adam le 9 Déc 2019
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by