MATLAB Answers


Attempt to execute SCRIPT varargin as a function

Asked by Elie Elias on 10 Nov 2018 at 21:27
Latest activity Commented on by Walter Roberson
on 10 Nov 2018 at 22:23
when i run my function i get
Attempt to execute SCRIPT varargin as a function:
C:\Program Files\MATLAB\R2018a\toolbox\matlab\lang\varargin.m
I dont know what im doing wrong im very new to matlab and dont completely unerstand how varargin and varargout work). Thanks
function [C]=calculerCharges_2(coefs,varargin)
for i=1:11:length(varargin)
varargin{i+1}=input('Entrez l''âge de la personne');
s=input('Entrez le sexe de la personne (m ou f)','s');
if s=='f'
varargin{i+3}=input('Entrez son IMC');
varargin{i+4}=input('Entrez le nombre d’enfants que cette personne a');
f=input('Cette personne fume-t-elle ? (o ou n)','s');
if f=='n'
r=input('Où vit-elle ? (ne, nw, sw, se)','s');
if strcmp(r,'ne')
elseif strcmp(r,'nw')
elseif strcmp(r,'sw')
elseif strcmp(r,'ne')


Sign in to comment.

2 Answers

Answer by Walter Roberson
on 10 Nov 2018 at 21:32
Edited by Walter Roberson
on 10 Nov 2018 at 21:36
 Accepted Answer

You are attempting to use varargin inside a script. varargin is only defined inside a function.
Perhaps your line
should be
function [C]=calculerCharges_2(coefs,varargin)
so that you would be defining a function.
I notice you have
for i=1:size(varargin,1)
varargin is always either empty, with size 0 0, or else is a row vector, size 1 x number of arguments. Therefore, size(varargin,1) will be either 0 (no arguments) or 1 (non-empty row vectors have one row no matter how many elements are in the row.)


for some reason it didnt paste correctly but it was already
function [C]=calculerCharges_2(coefs,varargin)
my bad there
alright thanks for pointing this out. I changed my code but it still says Attempt to execute SCRIPT varargin as a function. I edited my question with my latest code.
I put in your code and used some random input and do not encounter any difficulty.
I think your code is poorly structured, but I do not encounter that error.

Sign in to comment.

Answer by Cris LaPierre on 10 Nov 2018 at 21:53
Edited by Cris LaPierre on 10 Nov 2018 at 21:53

If this is the complete code of your function, you are missing the "|function|" declaration. Without it, it is trying to run your code as a script instead of a function. Try starting your code with
function [C]=calculerCharges_2(coefs,varargin)
Your use of varargin is not as I would expect or recommend. It is used as an input to your function when the number of inputs may not always be the same. It allows you to call a function without having to pass in every input every time. Like the size function you use in your code, I can call it with size(var) of size(var, 1). This is what varargin allows you to do.
Similarly, varargout allows a function to return a variable number of output arguments depending how many outputs are asked for in the calling statement. Using the same function, size, both of these syntaxes are valid.
[m,n] = size(A)
sz = size(A)
Without these, your function would throw an error message when the function were called with anything less than the full complement of input and output arguments.


the thing is that i want to use varargin(ex: x=varargin{}) but i also want a message asking the user for something before each input(ex: x=input('')). I dont know how to do that so i used varargin{}=input(). Is there a better way to do that?
The way you are writing your function, the only input you are using is coefs. There is no need for varargin. To use it correctly, varargin would be on the right side of the "=".
val = varargin{i}
"the only input you are using is coefs"
Not exactly. The number of arguments is also important; it determines how many times the code loops prompting for values, which in turn determines the length of the output.
>> calculerCharges_2(1,2,3,4,5,6,7,8,9,10,11,12,13)
Entrez l'âge de la personne45
Entrez le sexe de la personne (m ou f)f
Entrez son IMC35
Entrez le nombre d?enfants que cette personne a0
Cette personne fume-t-elle ? (o ou n)o
Où vit-elle ? (ne, nw, sw, se)sw
Entrez l'âge de la personne2
Entrez le sexe de la personne (m ou f)f
Entrez son IMC0
Entrez le nombre d?enfants que cette personne a8
Cette personne fume-t-elle ? (o ou n)n
Où vit-elle ? (ne, nw, sw, se)ne
ans =
2 1 0 35 0 1 0 0 0 1 0 13 1 0 0 8 0 1 1 0 0 0

Sign in to comment.