# Error using (function_name). Too many output arguments.

32 views (last 30 days)
Luigi on 24 Apr 2024
Commented: Deepu on 25 Apr 2024
I'm having some issues with this code:
clear
clc
fluido = "aria"
fluido = "aria"
i=0;
for temp = 15:0.1:1500
i=i+1;
temperatura(i)=temp;
vettore_cp(i)=calcolo_cp(temp, fluido);
end
Error using solution>calcolo_cp
Too many output arguments.
plot(temperatura,vettore_cp,"b-")
title("grafico del calore specifico in funzione della temperatura")
xlabel("temperatura [°C]")
ylabel("calore specifico [J/kg*K]")
The program should plot a diagram using the function calcolo_cp that calculates the specific heat and uses as inputs the temperature of the fluid and what type of fluid we want to examinate ( in this case "aria", "air" in english).
The function calcolo_cp is in the same folder and seems to work fine, the code of this function is:
function calcolo_cp(T,fluido)
if strcmp(fluido, "aria")
indice_riga=1;
elseif strcmp(fluido, "gas")
indice_riga=2;
else
disp("ERRORE FLUIDO")
end
coeff=[1000.61 0.482759e-1 0.462906e-3 -0.454035e-6 0.129112e-9;
1013.71 0.641882e-1 0.452287e-3 -0.444350e-6 0.125765e-9];
for i = 1:5
elementi_cp(i) = coeff(indice_riga,i)*T^(i-1);
end
cp = sum(elementi_cp)
end
Thank you all.

Stephen23 on 24 Apr 2024
Edited: Stephen23 on 24 Apr 2024
You did not define the function to return any output arguments:
function calcolo_cp(T,fluido)
If you intended to return e.g. CP then you need to tell MATLAB that:
function cp = calcolo_cp(T,fluido)
The documentation explains how to define functions: https://www.mathworks.com/help/matlab/ref/function.html

Deepu on 24 Apr 2024
The error message "Too many output arguments" typically occurs when the number of variables you are trying to assign the outputs of a function to does not match the number of output arguments the function actually returns.
In your code, the function calcolo_cp is called inside a loop, and you are trying to assign its output to temp, which is likely causing the error. The calcolo_cp function seems to only calculate a single output, cp, but your code expects two outputs (temp and cp).
To fix this issue, you need to ensure that you are assigning the correct number of output arguments from the calcolo_cp function. If you only need cp, you can modify your loop as follows:
for temp = 15:0.1:1500
cp = calcolo_cp(temp, fluido);
vettore_cp(i)=cp;
end
This way, you're only assigning the output cp to vettore_cp(i), which should resolve the "Too many output arguments" error.
Stephen23 on 24 Apr 2024
"The calcolo_cp function seems to only calculate a single output, cp, but your code expects two outputs (temp and cp)."
Really? It seems like your AI engine does not understand MATLAB code syntax.
Deepu on 25 Apr 2024
My interpretation was incorrect. The error message indeed indicates that the function call is returning too many output arguments than expected.
Thank you for bringing this to my attention, and I appreciate your patience.