if/elseif script problem
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Daniel Shub
2012-3-25
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!