Function returning ans instead of three outputs

My function is:
function [R,G,B] = MostDistantPixel(pixels)
%rest of code
R = ipixel(1)
G = ipixel(2)
B = ipixel(3)
On the command window it says R = 50, G = 48, and B= 47.
However, it is not returning these outputs and returning only the first output R with a value of 50. How do I fix this?

 Réponse acceptée

Stephen23
Stephen23 le 8 Sep 2019
Modifié(e) : Stephen23 le 25 Nov 2020
You need to call the function with three output arguments, e.g.:
inp = [...]; % your input array
[RR,GG,BB] = MostDistantPixel(inp)
Useful basic MATLAB concepts, such as how to call functions with multiple input/output arguments, are explained in the introductory tutorials:
Note that if a function that returns an output and it is called without any outputs being allocated to a variable, then its first output will be assigned to the default variable ans:
It is recommended to avoid referring to ans in code: it is recommended to always define your own output variables.
PS: You will probably also want to suppress the displaying of R, G, and B using semi-colons.

7 commentaires

Thank you! Although why do we use [RR,GG,BB] instead of [R,G,B]?
Stephen23
Stephen23 le 8 Sep 2019
Modifié(e) : Stephen23 le 8 Sep 2019
"Although why do we use [RR,GG,BB] instead of [R,G,B]?"
I just did that to emphasize the fact that the names in the calling workspace do NOT have to be the same as the names used inside the function. You can name the variables however you want.
The names used inside a function are (and should be) irrelevant to what you call your input/output variables. Do you know the names of internal variables used in sin? (hint: no). Does that stop you from defining any input/output variable names and calling sin? (hint: no).
someInputName = [...]
someOutputArg = sin(someInputName)
I doubt that sin uses either of those names internally, but calling the function works perfectly.
That is the point of functions.
Another question is when I give the function an input using the command window, but I only click run, it gives me an answer of 50. However, if I enter the input AND enter [R,G,B] = MostDistantPixel(pixels), I get the correct answer with 3 different outputs. Why is that?
Stephen23
Stephen23 le 8 Sep 2019
Modifié(e) : Stephen23 le 8 Sep 2019
"...but I only click run..."
Ugh, that awful green Run button. It is the cause of much misunderstanding. Avoid it.
By default it calls functions without any inputs and without assigning the outputs to any variables. You know what happens to a function which is called without the outputs being assigned to any variables: the first output is assigned to ans by default.
It is possible to set up the Run button to call functions with particular inputs (click on its down-arrow), but really you are better off simply calling your function the normal way, by writing it in the command line (with all input/output variables) and then pressing enter (or calling it from a script/function/...).
This makes so much sense! So let’s say I use this function in another function, it will return the three outputs? Sorry, quite new to matlab.
Stephen23
Stephen23 le 8 Sep 2019
Modifié(e) : Stephen23 le 8 Sep 2019
"So let’s say I use this function in another function, it will return the three outputs?"
Where you call it from makes absolutely no difference: if you define your function with three output arguments then when you call it you can allocate those three outputs to three variables. It makes no difference if you do this from the command line, inside another function, or in a script: the syntax is always the same (just like I showed in my answer).
Note that you can also call a function with fewer outputs than the function defines, the unassigned outputs are then essentially ignored (however in some cases, e.g. meshgrid, ndgrid, find, size, etc., the number of output arguments used can also change the functionality).
Thank you! This has helped me a lot.

Connectez-vous pour commenter.

Plus de réponses (1)

Povilas Simonis
Povilas Simonis le 25 Avr 2021

0 votes

I have similar problem, yet I dont manage to understand how to fix it..
function [Out] =test2(a)
if a>3
Out=a+1;
else
fprintf('Less than 4\n');
end
function gives me ans instead of Out

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by