Advanced MATLAB: Varargin and Nargin- variable inputs to a function22

Posted by Doug Hull,

Most MATLAB functions are overloaded. That means that there are several ways to call them and that MATLAB will “Do The Right Thing” based on the number and types of inputs. As a simple example you can call SUM

sum(M) %this will sum along the columns sum(M,2) %this will sum along the rows

This video shows how you can implement the same behavior based on using

VARARGIN: VARiable ARGuments IN
NARGIN: Number ARGuments IN

Other videos have been gathered here:
http://blogs.mathworks.com/pick/category/video/

Other Advanced MATLAB posts have been gathered here:

Note

Justin replied on : 1 of 22

Can your output ever be a text and not a number?

Markus replied on : 2 of 22

Hi!

I sometimes use “exist” in order to realize variable inputs:

function bla(A,B,D)
if ~exist(‘D’, ‘var’)
D = 10; % default value
end

Here, I did not hard-code the order and number of input arguments. If I ever want to introduce an additional input C before D, like

function bla(A,B,C,D)

I do not have to change anything (except the function calls of course). I think both alternatives have their pros and cons.

Regards
Markus

Doug replied on : 3 of 22

Justin,

You can use num2str to make a number into text. You can have a function output be string if that is what you want.

Doug

Luca Balbi replied on : 4 of 22

Hello Doug, great work as always!

I think that the recent introduction of the inputParser object has made things evolve a bit with respect to the nargin approach.
Have you ever thought about creating a video with a demo of inputParser?

dylan replied on : 5 of 22

i followed your code exactly and i get the following error:
>> main(1)
??? Undefined function or method ‘mtimes’ for input arguments of type ‘cell’.

Error in ==> main at 19
out=alpha * beta;

Jiro replied on : 6 of 22

dylan,

Make sure you are using (curly) braces and not parentheses:

alpha = varargin{1} 

“varargin” is a cell array, so you need to use braces to get the contents of each element. Parentheses will only give you back the element as a cell.

siva replied on : 7 of 22

i have a doubt in using varargin. if i give this as it is in the calling function, i get “Attempted to execute script varargin as a function”. if i omit varargin, it gives the error as “One or more output arguments not assigned during call to…”. what should i have to do? plz send the answer to my mail soon. thanks in advance

Doug replied on : 8 of 22

Siva,

Please post the actual code, the function, and how you call it. You can simplify the function so it is shorter for this purpose.

Doug

Seun replied on : 9 of 22

Hi Doug,

Sorry to trouble you, I am trying to plot a 3axis(XYZ) bar graph with some data I have in excel, would attach the data here but it doesn’t seem possible to.

x-Height
y-Ergo_Index
z-Distance

I also want to plot the mean, standard deviation +/- of the Ergo_Index on the same graph.

Could you point me in the right direction, I am not too good at matlab, I have tried the gui method to do this but never seem to get a suitable graph or one in 3d either.

Thanks

Seun replied on : 10 of 22

Thanks for your reply, I tried the steps you had in your tutorial, I actually don’t know how to use matlab so I am learning on the go.

I have been able to import the data but when i try to plot the graph i seem to get line graphs which are not correct.

I have posted sample data below so i can explain better what i am trying to plot

I will go over the links you sent again.

Thanks

x-Height
y-Ergo_Index, Ergo_Index_+Sta. Dev., Ergo_Index_-Sta. Dev.
z-Distance

Height Distance Ergo_Index Eg_Index_+SDev. Erg_Index_-SDev.
1 1 8.02810 8.48252 7.57368
1 1 7.23410 7.68852 6.77968
1 1 6.77290 7.22732 6.31848
1 1 7.26650 7.72092 6.81208
1 1 7.85150 8.30592 7.39708
1 1 7.44710 7.90152 6.99268
1 2 6.85090 7.92199 5.77981
1 2 7.54950 8.62059 6.47841
1 2 8.84650 9.91759 7.77541
1 2 7.35340 8.42449 6.28231
1 2 9.51260 10.58369 8.44151
1 2 8.99790 10.06899 7.92681
1 3 7.90830 8.11751 7.69909
1 3 7.92300 8.13221 7.71379
1 3 7.99560 8.20481 7.78639
1 3 8.15680 8.36601 7.94759
1 3 7.87130 8.08051 7.66209
1 3 8.08800 8.29721 7.87879
1 3 8.34180 8.55101 8.13259
1 3 7.64370 7.85291 7.43449
2 1 9.67030 10.07334 9.26726
2 1 8.95520 9.35824 8.55216
2 1 8.68640 9.08944 8.28336
2 1 8.75610 9.15914 8.35306
2 1 8.99480 9.39784 8.59176
2 1 9.44720 9.85024 9.04416
2 1 9.73000 10.13304 9.32696
2 1 9.30680 9.70984 8.90376
2 2 7.59970 7.85001 7.34939
2 2 7.38770 7.63801 7.13739
2 2 7.45730 7.70761 7.20699
2 2 7.86410 8.11441 7.61379
2 2 7.61410 7.86441 7.36379
2 2 7.70390 7.95421 7.45359
2 2 8.15410 8.40441 7.90379
2 2 7.87890 8.12921 7.62859
3 3 7.28780 7.86754 6.70806
3 3 7.57240 8.15214 6.99266
3 3 8.52170 9.10144 7.94196
3 3 7.88650 8.46624 7.30676
3 3 7.39120 7.97094 6.81146
3 3 8.50380 9.08354 7.92406
3 3 8.30720 8.88694 7.72746
3 3 8.81800 9.39774 8.23826

Doug replied on : 11 of 22

Seun,

For us to give you some help, you will need to show us what you tried, in code. There are many things that could have been done incorrectly. The data you have shown would plot well.

Doug

Debasish Sarker replied on : 13 of 22

I have a problem.I am making a code using MatLab. This code has some subfunctions.In these subfunctions, sometimes I need to call several numbers of value under one variable name and I want to do this for more than one variable in a subfunction.But I found that one variable can hold only one value. Is there any solution? REply please

Doug replied on : 14 of 22

@Debasish,

Very often people will use structures

a.alpha = 1;
a.beta = 2;

Cells work too

a{1} = magic(3);
a{2} = ‘Doug’;

Enjoy,
Doug

yan yan replied on : 15 of 22

Hi,I use a free program to do some tidal analysis.But there are some errors when I run it. Attached is the the beginning of the free program.
After I test ‘error(nargchk(2,inf,nargin))’,
the error informatio shows ‘??? Error using ==> nargin Not a valid M-file.’

Can you help troubleshoot it? Thank you in advance.

options = struct(‘logp’,false); % default options
if ischar(x) && strcmpi(x,’defaults’)
f = options;
return
end

error(nargchk(2,inf,nargin))

Doug replied on : 16 of 22

@yan,

I am not sure, but I think inf might be the problem for the upper limit on the number of arguments. Can you change it to something like 100 and see if it still fails?

I would put a breakpoint on that line and investigate the values of nargin to make sure it is what you think it is.

Doug

natasha replied on : 17 of 22

function [Sx, alphao, fo] = autossca(x,fs,df,dalpha)
if nargin ~= 4

error(‘Right number of arguments’);
end
Np=pow2(nextpow2(fs/df));
L=Np/4;
P=pow2(nextpow2(fs/dalpha/L));
N=P*L;
if length(x)N
x=x(1:N);
end
NN=(P-1)*L+Np;
xx=x;
xx(NN)=0;
xx=xx(:);
X=zeros(Np,P);
for k=0:P-1
X(:,k+1)=xx(k*L+1:k*L+Np);
end
a =hamming(Np);
XW=diag(a)*X;
XF1=fft(XW);
XF1=fftshift(XF1);
XF1=[XF1(:,P/2+1:P) XF1(:,1:P/2)];
E=zeros(Np,P);
for k=-Np/2:Np/2-1
for m=0:P-1
E(k+Np/2+1,m+1)=exp(-1i*2*pi*k*m*L/Np);
end
end
XD=XF1.*E;
XR=zeros(Np,P*L);
for k=1:P
XR(:,(k-1)*L+1:k*L)= XD(:,k)*ones(1,L);
end
xc=ones(Np,1)*x’;
XM=XR.*xc;
XM=conj(XM’);
XF2=fft(XM);
XF2=fftshift(XF2);
XF2=[XF2(:,Np/2+1:Np) XF2(:,1:Np/2)];
M=abs(XF2);
alphao=(-1:1/N:1)*fs;
fo=(-.5:1/Np:.5)*fs;
Sx=zeros(Np+1,2*N+1);
for k1=1:N
for k2=1:Np
alpha=(k1-1)/N+(k2-1)/Np-1;
f=((k2-1)/Np-(k1-1)/N)/2;
k=1+Np*(f+.5);
l=1+N*(alpha+1);
Sx(k,l)=M(k1,k2);
end
end

Doug replied on : 18 of 22

@Natasha

Line three is an error command, if it throws an error, it is working as intended.

Doug

Ferran replied on : 19 of 22

Hey!

Thank you very much. I cleared a lot of doubts about “varargin{}”. Now I can do my work!

Thank you again!

Ferran.

mhy replied on : 20 of 22

Hi. recently i came across the following coding:
error(nargchk(3,5,nargin));
[cax,args,nargs] = axescheck(varargin{:});

would u tell me “in plain english” what is this code all about, what can it do, and what kind of keyin input or somethin?

sorry if it sounds silly but i am still new with matlab. Appreciate any help you can give.

Constantine replied on : 21 of 22

A Million Thanks!!!

negar replied on : 22 of 22

how we can read a FSA file in Matlab?