Inverse input/output of switch case function
Show older comments
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 Comments
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
Accepted Answer
More Answers (1)
Dyuman Joshi
on 15 Jun 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
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!