Trying to make a function who says the position for a letter in the alphabet

3 views (last 30 days)
I can't figure out how to get this fuction to work. I need to use a letter as input, for example a, and the funkttion to return its position (1).
function h = postiton(a)
Alfabetet=['a';'b';'c';'d';'e';'f';'g';'h';'i';'j';'k';'l';'m';'n';'o';'p';'q';'r';'s';'t';'u';'v';'w';'x';'y';'z';];
h=find(Alfabetet==a))
end
How do i get this to work?
I want to write position(k) and get ans=11

Accepted Answer

Image Analyst
Image Analyst on 2 Apr 2014
Try this:
function test
aPosition = postiton('a')
function h = postiton(theLetter)
Alfabetet=['a';'b';'c';'d';'e';'f';'g';'h';'i';'j';'k';'l';'m';'n';'o';'p';'q';'r';'s';'t';'u';'v';'w';'x';'y';'z';];
h = find(ismember(Alfabetet, theLetter));
  2 Comments
John
John on 2 Apr 2014
Edited: John on 2 Apr 2014
This works if you use position('letter'). Can I rewrite it so that it will work with just position(letter).
Big thanks to you from sweden!
Image Analyst
Image Analyst on 2 Apr 2014
Not if it's a literal but you can if it's a variable, like this:
a = 'c'; % Whatever letter you want.
aPosition = postiton(a) % or position() - however you want to spell it.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming Utilities 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!