problem in return of function

Hi, I have the following code
function [c,d]=test2(a,b)
if a>1
c=a*b';
d=a+b;
else
c=a*b'
end
In case of a be >1 I got this error message: Error in ==> test2 at 2 if a>1
??? Output argument "d" (and maybe others) not assigned during call to
"D:\MATLAB\R2011a\bin\test2.m>test2".
thanks
[EDITED, Jan, code formatted]

11 comentarios

Yash
Yash el 1 de Sept. de 2012
there should be a value of d in the else statement in case the situation if the number a is <1 so some value should be assigned to d.
Yash
Yash el 1 de Sept. de 2012
but the code is working for the case of a>1
[c,d]=test2(2,4)
c =
8
d =
6
huda nawaf
huda nawaf el 1 de Sept. de 2012
Many thanks for all
Jan
Jan el 1 de Sept. de 2012
@huda: You still refuse to format the code correctly. I really hoped, that the discussion in http://www.mathworks.com/matlabcentral/answers/46882-i-can-not-find-important-function encourages you to follow the conventions. The more unclear and bad formatted question are posted in this forum, the smaller is the motivation of high skilled Matlab profis to participate in this forum frequently.
Walter Roberson
Walter Roberson el 1 de Sept. de 2012
When I end up having to format messages for someone repeatedly, they have to be asking something pretty interesting for me to also spend time thinking about their questions.
huda nawaf
huda nawaf el 2 de Sept. de 2012
where is the problem now in my code . I wrote it then press {} and do spaces among steps.
tell me please , where is the error?
Matt Fig
Matt Fig el 2 de Sept. de 2012
Huda, you have to highlight the code, then while it is highlighted push the {} Code button.
Jan
Jan el 2 de Sept. de 2012
Editada: Jan el 2 de Sept. de 2012
@huda: Do you remember that we suggested some links which explain the code formatting exhaustively? Now you want us to explain this again, although our experiences look like you are not going to care?! That's bold. So let me see what happens, if I post the tips again:
  • One empty line before the code
  • Two spaces in front of each line of code
  • Standard indentation, e.g. as performed by Ctrl-I in Matlab's editor
  • Extra empty lines in the code only to separate sections of code, not after each line
  • One empty line after the code
Alternatively:
  • One empty line before and after the code
  • Mark the code and hit "{} code" - this inserts the same two spaces as if they were typed manually.
The full desctiption (again): Markup help.
Do you have a suggestion for me, what I or other editors could do, if the quality of your question still no gets higher? Incomplete and badly formatted questions are noise in a forum, which reduce the quality of the whole forum. If you find a solution for this problem, I think you would be the perfect candidate to remember other contributors to brush up the appearance of their postings.
huda nawaf
huda nawaf el 2 de Sept. de 2012
I saw that links of code , just mark the code then press {}code then make spaces between each two lines
Image Analyst
Image Analyst el 2 de Sept. de 2012
No. You don't need to "make spaces between each two lines" (each pair of lines). You just make sure there is a blank line before the first line of your code. Then you highlight ALL your code, which may include blank lines or not - it doesn't matter, just highlight everything. Then click {}Code and it will format everything properly, even going across chunks of your code where you have a blank line.
Oleg Komarov
Oleg Komarov el 2 de Sept. de 2012
@huda: then you weren't looking carefully. In the .gif I format the code, and then I improve it a bit. You can forget about the second part, just watch what happens when I press {}code in the preview.

Iniciar sesión para comentar.

Respuestas (3)

Star Strider
Star Strider el 1 de Sept. de 2012
Editada: Star Strider el 1 de Sept. de 2012

1 voto

If it is not always necessary for you to return a value for ‘d’, you might consider using the nargout function.
For example:
function [c,d]=test2(a,b)
if (nargout > 1) && (a > 1)
c=a*b';
d=a+b;
elseif (nargout == 1) && (a <= 1)
c=a*b'
end
So if your calling function calls test2 with (a <= 1), only needs ‘c’ and does not need ‘d’, this version of your function would only return a value for ‘c’. You would not generate the error. You can experiment with different options.
Image Analyst
Image Analyst el 1 de Sept. de 2012
Editada: Image Analyst el 1 de Sept. de 2012

0 votos

So when you pass in a value of a less than 1, what value does d have? Think about it. It could be written more robustly like this perhaps:
function [c ,d] = test2(a, b)
% Initialize outputs to null.
c = [];
d = [];
try
% b is apparently an array because he's using the transpose operator '.
% a must be a scalar since he's comparing it to 1.
% c is the same for both parts of the if so we can pull it out of the if.
% We have no idea what d was supposed to be in case a < 1
% So we'll just assign it to a string.
c = a * b';
if a > 1
d = a + b;
else
% Assign d to a string or whatever you want.
d = 'a is less than 1';
end
catch ME
errorMessage = sprintf('Error in function test2.\n.\n\nError Message:\n%s', ME.message);
uiwait(warndlg(errorMessage));
end
Yash
Yash el 1 de Sept. de 2012
Editada: Yash el 1 de Sept. de 2012

0 votos

for any values less then one it will give the following output
[c,d]=test2(-2,4)
c =
-8
Error in ==> test2 at 2 if a>1
??? Output argument "d" (and maybe others) not assigned during call to "D:\MakeYourassignment.com\test2.m>test2".
however change the code to this if you dont want any such error to exist..
function [c,d]=test2(a,b)
if a>1
c=a*b';
d=a+b;
else
c=a*b';
d=0;
end
now the output will be
[c,d]=test2(-2,4)
c =
-8
d =
0

Categorías

Etiquetas

Preguntada:

el 1 de Sept. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by