Clear Filters
Clear Filters

if/elseif script problem

1 view (last 30 days)
C Meek
C Meek on 25 Mar 2012
Hi there,
I am writing a function designed to take an input note, and return the relevant frequency. However, I keep hitting problems whenever I change the strings slightly.
Here is part of my example that works:
function [f]=note_to_freq(notename)
if notename == 'A'
f=110;
elseif notename == 'A#'
f=116;
....etc
end
Now, when I do something like change the elseif notename == 'A#2', Matlab seems to treat notename as a string and compare the two. This then returns the error message:
??? Error using ==> eq
Matrix dimensions must agree.
Any explanations for why this is happening/how to fix it?
Thanks in advance.
Craig

Accepted Answer

Daniel Shub
Daniel Shub on 25 Mar 2012
I would suggest you look into
doc switch
This might be better than lots of if/elseif/elseif...
You should also look at
doc strcmp
As for what is happening, == (which is really the eq function) doesn't like things that are different sizes). Basically, you are trying to do
[65, 35] == [65, 35, 50]
which is obviously confusing. A better fit would be isequal (which will avoid the error), but strcmp is best.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!