How to create user defined function in matlab?

I have use below set of code frequently. So i have to make user defined function
below is my frequently used code:-
x=xvalue;
y=yvalue;
for j=1:3;
red(j)=RGB(y,x,j);
end
shape.color=red;

2 comentarios

Azzi Abdelmalek
Azzi Abdelmalek el 22 de En. de 2014
Can you explain what are the argument (inputs and outputs) of your function?
saravanakumar D
saravanakumar D el 22 de En. de 2014
Editada: saravanakumar D el 22 de En. de 2014
input is x and y values
output is pixel color value -->shape.color= [242 0 12]

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 22 de En. de 2014
Editada: Image Analyst el 22 de En. de 2014
I think this is the minimum necessary:
function shape = myFunction(RGB, xvalue, yvalue, i)
x=xvalue;
y=yvalue;
for j=1:3;
red(j)=RGB(y,x,j);
end
shape(i).color=red;
Optionally, x, y, and red could also be outputs, and shape could also be an input. Be aware that the "red" variable actually contains the red, green, and blue values from the pixel at (y, x).

6 comentarios

saravanakumar D
saravanakumar D el 22 de En. de 2014
Editada: saravanakumar D el 22 de En. de 2014
You didn't store the result value in to variable shape, then how it gives output?
saravanakumar D
saravanakumar D el 22 de En. de 2014
Editada: saravanakumar D el 22 de En. de 2014
Is below code is correct? And i don't need i, so i neglect
function shape = myFunction(RGB, xvalue, yvalue)
x=xvalue;
y=yvalue;
for j=1:3;
red(j)=RGB(y,x,j);
end
shape.color=red;
shape=shape.color;
Walter Roberson
Walter Roberson el 22 de En. de 2014
No, the last line is wrong. You cannot have two assignments in a single expression.
saravanakumar D
saravanakumar D el 22 de En. de 2014
I edited now.
shape.color=red;
creates "shape" as a structure with a field "color" that it sets to the content of "red". Then your statement
shape=shape.color
overwrites "shape" with the contents of the field "color", into which you had written the content of "red". The net effect of your code is as the same as if you had changed those last two lines to
shape = red;
Image Analyst
Image Analyst el 23 de En. de 2014
Do you want a structure or not? If there is only one field, then I see no reason at all to use a structure. Just use a simple variable.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 22 de En. de 2014
function shape = myFunction(RGB, xvalue, yvalue, i)
shape(i).color = squeeze(RGB(xvalue, yvalue, :));

2 comentarios

saravanakumar D
saravanakumar D el 22 de En. de 2014
which is output variable shape or shape(i).color?
Walter Roberson
Walter Roberson el 22 de En. de 2014
The output variable is "shape", as listed in the function header. The "shape" that is output will be a structure array with a single field "color", with the "i"th element of the structure array populated with meaningful data and the rest of the shape(K).color will be the empty array []

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 22 de En. de 2014

Comentada:

el 23 de En. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by