Filter löschen
Filter löschen

select string by condition

3 Ansichten (letzte 30 Tage)
Yona
Yona am 8 Mär. 2015
Kommentiert: Yona am 8 Mär. 2015
if i want select value by condition i can write
(a==1)*3+(a~=1)*5
when a will get 3 if a is 1 or 5 if not.
can i do somthing like this but the result is a string. foe example:
(a==1)*'Good'+(a~=1)*'bad'
when a will be 1 i will get 'good' else i will get 'bed'
i want it for sprintf. i want write a message that if a value is lower from 0 it will write 'loss' and if it higher it write 'profit'
sprintf('Are %s is %d', (v<0)loss (v>0)profit, v)

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 8 Mär. 2015
Yona - I think that the closest you can get to doing what you want is to create an array of strings as
msgs = {'loss','profit'};
then access the one you want based on whether v is positive or negative
sprintf('Our %s is %d', msgs{(v>0)+1}, v)
Try the above and see what happens!
  1 Kommentar
Yona
Yona am 8 Mär. 2015
good way. I didn't think about place in cell by condition

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by