필터 지우기
필터 지우기

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 CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by