Inverse input/output of switch case function
Mostrar comentarios más antiguos
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 comentarios
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
Respuesta aceptada
Más respuestas (1)
Dyuman Joshi
el 15 de Jun. de 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 comentario
Rub Ron
el 15 de Jun. de 2023
Categorías
Más información sobre Data Type Identification en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!