hi guys, new to matlab and came across a simple problem that takes me forever to get over. please help.
if rgb(1)==1
if rgb(2)==1
if rgb(3)==1
color='white';
else
color='yellow';
end
else
if rgb(3)==1
color='magenta';
else
color='red';
end
end
else
if rgb(1)==1
if rgb(3)==1
color='cyan';
else
color='green';
end
else
if rgb(3)==1
color='blue';
else
color='invalid input';
end
end
end
so its pretty much just mix three primary colors as input and output the mixed color.. i have a hard time going through this and i don't know where i did wrong.. thanks for answering. oh and here's the error:
Attempted to access rgb(3); index out of bounds because numel(rgb)=1.
Error in rgb_color (line 26) if rgb(3)==1

 Respuesta aceptada

Ilham Hardy
Ilham Hardy el 30 de Oct. de 2015
Editada: Ilham Hardy el 30 de Oct. de 2015

0 votos

What is your rgb value?
The error message is very clear. it stated that you tried to access the third index of rgb, whilst rgb only contains one value ( numel(rgb)=1).
Btw, you can also use logical operator instead of nested if-else statement

5 comentarios

Ilham Hardy
Ilham Hardy el 30 de Oct. de 2015
What I meant is what do you get when you type
rgb
in your command window?
Are you sure that your rgb contains three elements? 1x3 matrix?
Stephen23
Stephen23 el 30 de Oct. de 2015
@dingi duan: Ilham Hardy asked you a very simple but important question, because the answer to that question will help us to tell you what is wrong with your code. Here is the question again:
what do you get when you type
rgb
in your command window? (and press enter)
dingyi duan
dingyi duan el 30 de Oct. de 2015
it says" Undefined function or variable 'rgb'."....
Walter Roberson
Walter Roberson el 30 de Oct. de 2015
How are you invoking your code? I can see your file is named rgb_color.m but how are you starting it running?
dingyi duan
dingyi duan el 30 de Oct. de 2015
alright guys.. i just found out whats wrong: its my input.. this code is fine.. thanks all for helping.

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 30 de Oct. de 2015
Editada: Stephen23 el 30 de Oct. de 2015

0 votos

If you want to look up colornames then use my FEX submission colornames:
It is very simple to use:
>> colornames('html',[0.4,0.1,0.6])
ans = 'Purple'
>> colornames('html',[0.8,0.9,0.3])
ans = 'Yellow'
>> colornames('html',[1,1,1])
ans = 'White'

1 comentario

dingyi duan
dingyi duan el 30 de Oct. de 2015
thx but sry i have to use nested if...

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Oct. de 2015

Comentada:

el 30 de Oct. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by