Asked by huda nawaf
on 1 Sep 2012

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]

Answer by Star Strider
on 1 Sep 2012

Edited by Star Strider
on 1 Sep 2012

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.

Sign in to comment.

Answer by Image Analyst
on 1 Sep 2012

Edited by Image Analyst
on 1 Sep 2012

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

Sign in to comment.

Answer by Yash
on 1 Sep 2012

Edited by Yash
on 1 Sep 2012

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

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 11 Comments

## Yash (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/47176-problem-in-return-of-function#comment_97026

## Yash (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/47176-problem-in-return-of-function#comment_97027

## huda nawaf (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/47176-problem-in-return-of-function#comment_97033

## Jan (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/47176-problem-in-return-of-function#comment_97070

## Walter Roberson (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/47176-problem-in-return-of-function#comment_97073

## huda nawaf (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/47176-problem-in-return-of-function#comment_97094

## Login_Name (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/47176-problem-in-return-of-function#comment_97095

## Jan (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/47176-problem-in-return-of-function#comment_97119

## huda nawaf (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/47176-problem-in-return-of-function#comment_97160

## Image Analyst (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/47176-problem-in-return-of-function#comment_97161

## Oleg Komarov (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/47176-problem-in-return-of-function#comment_97162

Sign in to comment.