Borrar filtros
Borrar filtros

Use cells or char with if command

8 visualizaciones (últimos 30 días)
Luis
Luis el 5 de Abr. de 2012
Hello, I have a problem when passing a char character into a function, for instance, lets have a script
% run.m
y='negros'
x=randomfunction(y)
and the function
function x=randomfunction(y)
if y=='blancos'
x=5
elseif y=='negros'
x=0
end
But it does not work, I get
??? Error using ==> eq
Matrix dimensions must agree.
Error in ==> funcion at 3
if y=='blancos'
Error in ==> prueba at 4
x=funcion(y)
Can you guess how to fix this?
Thank you very much.

Respuestas (1)

Oleg Komarov
Oleg Komarov el 5 de Abr. de 2012
String comparisons should be carried out with strcmp
if strcmp(y,'blancos')
x = 5;
elseif strcmp(y,'negros')
x = 0;
end
Alternatively, using switch:
switch y
case 'blancos'
x = 5;
case 'negros'
x = 0;
end

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by