Attempt to execute SCRIPT varargin as a function
3 views (last 30 days)
Show older comments
Elie Elias
on 10 Nov 2018
Commented: Walter Roberson
on 10 Nov 2018
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+1}=1;
varargin{i+2}=0;
else
varargin{i+1}=0;
varargin{i+2}=1;
end
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'
varargin{i+5}=0;
varargin{i+6}=1;
else
varargin{i+5}=1;
varargin{i+6}=0;
end
r=input('Où vit-elle ? (ne, nw, sw, se)','s');
if strcmp(r,'ne')
varargin{i+7}=1;
varargin{i+8}=0;
varargin{i+9}=0;
varargin{i+10}=0;
elseif strcmp(r,'nw')
varargin{i+7}=0;
varargin{i+8}=1;
varargin{i+9}=0;
varargin{i+10}=0;
elseif strcmp(r,'sw')
varargin{i+7}=0;
varargin{i+8}=0;
varargin{i+9}=1;
varargin{i+10}=0;
elseif strcmp(r,'ne')
varargin{i+7}=0;
varargin{i+8}=0;
varargin{i+9}=0;
varargin{i+10}=1;
end
end
v=cell2mat(varargin);
C=v*coefs;
end
0 Comments
Accepted Answer
Walter Roberson
on 10 Nov 2018
Edited: Walter Roberson
on 10 Nov 2018
You are attempting to use varargin inside a script. varargin is only defined inside a function.
Perhaps your line
[C]=calculerCharges_2(coefs,varargin)
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.)
3 Comments
Walter Roberson
on 10 Nov 2018
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.
More Answers (1)
Cris LaPierre
on 10 Nov 2018
Edited: Cris LaPierre
on 10 Nov 2018
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.
3 Comments
Cris LaPierre
on 10 Nov 2018
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}
Walter Roberson
on 10 Nov 2018
"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
See Also
Categories
Find more on Install Products in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!