0

If I define a function like this

function [x, y] = findXY(B)
   [by, bx] = size(B);
   x = zeros(by,bx);
   y = zeros(by,bx);
   for i=1:10
       x(i) = i;
       y(i) = i; 
   end
end

I get the following error

The constructor for class 'findXY' must return only one output value.

Why is this happening? I can't use return values in a loop? I didn't find something similar while googling.

1
  • 3
    What version of MATLAB are you using? How are you calling this function and how is it defined? Seems to work fine for me on R2009b when this function is alone in an m-file. Commented Feb 21, 2012 at 13:55

3 Answers 3

1

I have tried it:

[x, y] = findXY(ones(10,10))

and there is no problem. Could you give a more detailed explanation about where and how are you using it?

Sign up to request clarification or add additional context in comments.

Comments

0

Probably the function is considered as constructor of a class - does it maybe sit in a directory named @TestXY?

And in this case, you are only allowed to return one object.

Maybe you could wrap your two return values into one cell array...

Something like

function xy = findXY(B)
   [by, bx] = size(B);
   x = zeros(by,bx);
   y = zeros(by,bx);
   for i=1:10
       x(i) = i;
       y(i) = i; 
   end
   xy = {x y};
end

But if the function is not a constructor, your code is perfectly valid.

1 Comment

The directory name is "matlab". Does this make a difference? I can't return a single value cause I am rewriting a function that has this output and changing it will affect a lot of code.
0

I will answer my question. The problem was a bit weird. I just restarted matlab and everything worked as expected. So if you walk into trouble and you cant find what is going wrong you should give restarting a try. It may work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.