Borrar filtros
Borrar filtros

if/elseif script problem

2 visualizaciones (últimos 30 días)
C Meek
C Meek el 25 de Mzo. de 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

Respuesta aceptada

Daniel Shub
Daniel Shub el 25 de Mzo. de 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.
  1 comentario
C Meek
C Meek el 25 de Mzo. de 2012
Thanks! Got it working.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by