2

I use the following function in a Matlab program:

...   
...   
...  
[A, B, C, D, E] = function (F, G, H, I, J, K, L, M, N, O, P)  
...  
...  
...  

and I get the following error message:

??? Error using ==> function  
Too many output arguments.  

A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P are the vectors of inputs and outputs of the function.

but the same program works very well when I replaced the line of the function by its full script!

Can you tell me where I should look to find the error?

1
  • 2
    Can you post the exact definition of your function and the line where you call it? Commented Apr 9, 2010 at 12:29

8 Answers 8

5

Did you call your function function? This is a VERY BAD idea, since function is a reserved key word.

Assuming you have simply replaced the name of the function you want to call with 'function' in your example: You need to define input and output in the function definition. For example, for a function called 'myFun', which accepts F-P as inputs, and should return A-E as outputs, you write as the first line of the function

function [A,B,C,D,E] = myFun(F,G,H,I,J,K,L,M,N,O,P)

EDIT

To clarify: You get the error because you're asking for more output arguments than the function can supply. You'd get the same error if you'd call [u,v]=sin(0), since sin is defined with 1 output only. Thus, you have to check your function's signature to solve the problem.

EDIT 2

Let's make an example

I open the editor and define the function

function [A,B,C] = myFun(D,E,F)
%# myFun returs the pairwise sums of the input arguments

A = D+E;
B = E+F;
C = F+D;

Then, I save the function as "myFun.mat" on the Matlab path.

Now I can call myFun like so:

[A,B,C] = myFun(1,2,3);

However, if I call myFun with four output arguments, I'll get an error

[A,B,C,D] = myFun(1,2,3);

In fact, I get exactly the error you got, because I only defined myFun with three output arguments.

Note: You can always call a function with fewer than the number of defined output arguments, but never with more.

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

2 Comments

I named function only as an example but I do not use this term in my program.
I already use several functions in my Matlab programs but this is the first time I get this error message.
2

Matlab refuses to take assignments such as

[x,y]=[firstvalue,secondvalue] 

Try this in the Matlab shell. It will give you the same error.

Alternatively, do

z=[fv,sv]; x=z(1); y=z(2);

EDIT: Nevermind this answer. It does not suit your question.

Comments

0

I tried to test the program with a single output each time for A, B, C, D, E:
function [A] = myFun1 (F, G, H, I, J, K, L, M, N, O, P)
function [B] = myFun2 (F, G, H, I, J, K, L, M, N, O, P)
function [C] = myFun3 (F, G, H, I, J, K, L, M, N, O, P)
function [D] = myFun4 (F, G, H, I, J, K, L, M, N, O, P)
function [E] = myFun5 (F, G, H, I, J, K, L, M, N, O, P)

it works and the program works well, but the output is the only difference between the 5 functions myFun1, myFun2, myFun3, myFun4, myFun5.
I think this is not practical.
So is there any way to have a single function myfun with 5 outputs?

6 Comments

@lebland-matlab: yes, you can certainly write Matlab functions with 5 outputs, as @Jonas has shown you.
@ High Performance Mark: how? with [A, B, C, D, E] = function (F, G, H, I, J, K, L, M, N, O, P), I received the error message: ??? Error using ==> function Too many output arguments.
@ High Performance Mark: how? with [A, B, C, D, E] = MyFun(F, G, H, I, J, K, L, M, N, O, P), I received the error message: ??? Error using ==> function Too many output arguments.
I have edited my answer to try and make my point clearer: you need to define multiple output arguments in the FUNCTION DEFINITION so that you can have multiple output arguments in the FUNCTION CALL.
I defined 5 outputs vector for my function and I did not call myFun with more than 5 outputs!!!
|
0

I assume that your function really has 5 output arguments. If so, there is probably some other function with the same name in the Matlab Path with a different number of output arguments. Happens to me also now and then if I create functions with the same name but more or less output arguments.

As I see you load data from a specified path. Maybe you are not in the folder with the function you really want to call.

Try renaming your function (file + function name inside). Also try to Set Path to default (File->Set Path...-> Default) and change directory to the directory with your function. If none of this works then try all steps again, I'm pretty sure that's the reason for your errors.

Comments

0

@lebland

The code you posted defines a function with one return variable called matrice_return. It seems that you want to return the five variables AA,BB,CC,DD,EE. So now edit your function definition as @Jonas has explained very clearly. if you had posted your definition in your question we would have sorted this out hours ago.

Comments

0

Your problem is with the matrice_return variable. Instead of:

function matrice_return = myFun(Q,R,S,T,U,V,W,X,Y,Z,ZZ) 
.
.
.
matrice_return = [AA,BB,CC,DD,EE];

try:

function [AA,BB,CC,DD,EE] = myFun(Q,R,S,T,U,V,W,X,Y,Z,ZZ)

Comments

0

If you don't want to change myFun definition, you can do the following in your script:

myFun_result = myFun(F,G,H,I,J,K,L,M,N,O,P);
A = myFun_result(1:10);
B = myFun_result(11:20);
C = myFun_result(21:30);
D = myFun_result(31:40);
E = myFun_result(41:50);

Comments

0

my program:

clc;  
clear all;  

load('C:\Users\Documents\MATLAB\myFile\matrice_F.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_G.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_H.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_I.mat');  
F = m_F;  
G = m_G;  
H = m_H;  
I = m_I;  

load('C:\Users\Documents\MATLAB\myFile\matrice_J.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_K.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_L.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_M.mat');  
J = m_J;  
K = m_K;  
L = m_L;  
M = m_M;  

load('C:\Users\Documents\MATLAB\myFile\matrice_Result.mat');  
N = m_N ;  
O = m_O;  
P = m_P;  


[A,B,C,D,E] = myFun(F,G,H,I,J,K,L,M,N,O,P);  


file_name = 'matrice_final.mat';  
save(file_name,'A','B','C','D','E');  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        

my Function:

function %matrice_return%  [AA,BB,CC,DD,EE] = myFun(Q,R,S,T,U,V,W,X,Y,Z,ZZ)  

AA=zeros(1,10);  
BB=zeros(1,10);  
CC=zeros(1,10);  
DD=zeros(1,10);  
EE=zeros(1,10);  

for i=1:1:10  

        if  Q(i)>1   
            AA(i)=R(i)*S(i);  
        end  
        if  R(i)>1   
            BB(i)=T(i)*U(i);              
        end  
        if  S(i)>1   
            CC(i)=V(i)*W(i);              
        end  
        if  T(i)>1   
            DD(i)=X(i)*Y(i);              
        end  
        if  U(i)>1   
            EE(i)=Z(i)*ZZ(i);              
        end  
end  


%matrice_return = [AA,BB,CC,DD,EE];%  %%error

my problem is solved, I thank all those who gave their time to solve my problem.

I did not expect that the solution is so simple!!

1 Comment

@lebland: now do the decent thing and accept @Jonas's answer.

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.