Skip to content

Instantly share code, notes, and snippets.

@Thermoflux
Last active August 22, 2019 18:18
Show Gist options
  • Save Thermoflux/fa487b90d0998d115d56e231b376b86c to your computer and use it in GitHub Desktop.
Save Thermoflux/fa487b90d0998d115d56e231b376b86c to your computer and use it in GitHub Desktop.
Example/Tutorial for variable inputs/outputs in Matlab
function varargout = addNumbers(varargin)
% addNumbers is the name of the function.
%
% This file is intended to act as an example of how variable inputs/outputs
% are used in MATLAB
% ThermoFlux 2019
% For more information on varargin (variable num of arguments in input) type the following in command prompt.
% doc varargin
% doc nargin
% doc varargout
% nargin - (Num of arguments as input) is used to find how many inputs have been passed.
% Ex: addNumbers(2,3) whould give nargin = 2
% addNumbers(2,3,4) would give nargin = 3
% check to see if 2 inputs are given
if(nargin == 2)
a = varargin{1};
b = varargin{2};
c = 0;
elseif(nargin == 3) % if number of inputs are 3
a = varargin{1};
b = varargin{2};
c = varargin{3};
end
% do calculations here
result = a+b+c;
result2 = a+b;
result3 = a+c;
result4 = c+b;
% assign outputs here
% varargout - (Variable arguments in output) used to provide variable
% number of arguments as output to the user.
varargout = {result, result2, result3, result4};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment