lebland_Matlab
lebland_Matlab

Reputation: 29

Matlab Error: Too many output arguments

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?

Upvotes: 2

Views: 63470

Answers (8)

user2656907
user2656907

Reputation: 21

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.

Upvotes: 2

user312724
user312724

Reputation: 11

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!!

Upvotes: 0

yuk
yuk

Reputation: 19870

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);

Upvotes: 0

Doresoom
Doresoom

Reputation: 7448

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)

Upvotes: 0

High Performance Mark
High Performance Mark

Reputation: 78316

@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.

Upvotes: 0

George
George

Reputation: 3845

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.

Upvotes: 0

Jonas
Jonas

Reputation: 74930

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.

Upvotes: 5

user312724
user312724

Reputation: 11

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?

Upvotes: 0

Related Questions