Inverse input/output of switch case function
Afficher commentaires plus anciens
Say I have the following function:
function [y] = my_func(x)
switch x
case 'name a'
y = 'word i';
case 'name b'
y = 'word j';
case 'name z'
y = 'word k';
otherwise
error('error')
end
I would like to avoid to create a new function, and thus use this function(my_func) such that if I give the input 'word j' I get the output 'name b', or if I give the input 'word k' I get the output 'name z'.
Is there a way?
Thank you in advance for any hint.
3 commentaires
my_func('word j')
function [y] = my_func(x)
switch x
case 'word i'
y = 'name a';
% sprintf('%s',y)
case 'word j'
y = 'name b';
% sprintf('%s',y)
case 'word k'
y = 'name z';
% sprintf('%s',y)
otherwise
error('error')
end
end
Réponse acceptée
Plus de réponses (1)
Dyuman Joshi
le 15 Juin 2023
Add extra cases
function [y] = my_func(x)
switch x
case 'name a'
y = 'word i';
case 'name b'
y = 'word j';
case 'name z'
y = 'word k';
case 'word i'
y = 'name a';
case 'word j'
y = 'name b';
case 'word k'
y = 'name c';
otherwise
error('error')
end
1 commentaire
Rub Ron
le 15 Juin 2023
Catégories
En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!