フィルターのクリア

if/elseif script problem

1 回表示 (過去 30 日間)
C Meek
C Meek 2012 年 3 月 25 日
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

採用された回答

Daniel Shub
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.
  1 件のコメント
C Meek
C Meek 2012 年 3 月 25 日
Thanks! Got it working.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by