0
votes

I have a function which takes 11 pairs of input arguments (each pair representing a type of process and an input commodity) and calculates a result based on all of those 22 values. However, I would like to make the function flexible such that if I wanted to use, for example, 3 pairs of input arguments rather than 11, it would still come up with a result and meanwhile ignore the other 8 pairs of unused arguments. Can you please tell me the simplest way to do that?

I've tried using varargin but when I pass fewer than 22 values as the input arguments, MATLAB tells me that the 'the (varargin) index (in the function file) exceeds the marix dimensions.'

Is there any way to pass 'inactive' input arguments so that MATLAB simply doesn't perform any operation on them?

Thanks.

Here's a sample of my code in the function file:

function [ Eprout,AnnEprout,Demand,AnnDemand,Status,AnnStatus ] = supdem(time, country, weights,supdataset,output, demdataset,... process1,process2,process3,process4,process5,process6,process7,process8,process9,process10,process11,... ipcom1,ipcom2,ipcom3,ipcom4,ipcom5,ipcom6,ipcom7,ipcom8,ipcom9,ipcom10,ipcom11)

Eprout = (sum(sum(supdataset(time,country,process1,ipcom1,output)))+sum(sum(supdataset(time,country, process2,ipcom2,output)))+ ... sum(sum(supdataset(time,country,process3,ipcom3,output)))+sum(sum(supdataset(time,country, process4,ipcom4,output)))+ ... sum(sum(supdataset(time,country,process5,ipcom5,output)))+sum(sum(supdataset(time,country, process6,ipcom6,output)))+... sum(sum(supdataset(time,country,process7,ipcom7,output)))+sum(sum(supdataset(time,country, process8,ipcom8,output)))+ ... sum(sum(supdataset(time,country,process9,ipcom9,output)))+sum(sum(supdataset(time,country, process10,ipcom10,output)))+... sum(sum(supdataset(time,country,process11,ipcom11,output))))/1000;

1
It would help if you posted a little code, then we can probably resolve the trouble causing the error message. This might be better than the workaround you are seeking. - Buck Thorn
@Usman varargin sounds like the way to go indeed. Can you show the code that triggers the "index exceeds matrix dimensions" error? - Eitan T
Thank you both for your replies. I'm sorry I should have posted the code earlier. As you can see, the operations aren't performed directly on the additional input arguments. They just refer to elements in different structures, e.g., sum(supdataset(1,1,3,2,1)) returns a numerical value where 3 is process4 and 2 is ipcom4. I hope that clarifies my problem to some extent. - Usman

1 Answers

1
votes

There are a number of ways of doing this. A simple alternative to varargin is to pass a logical vector with 0/1 values. In your case, the vector would contain 11 entries. The value of each entry would signal to the function whether to expect that variable pair in a separate array. Within the function you could have a series of if statements to check whether an entry in v is 1 or 0, for instance:

function  [myoutputs] = myfunction(array_1,array_2,...., v);

default_1 = some_values1; % change some_values1 to whatever should be defaults for variable 2
default_2 = some_values2; % change some_values2 to whatever should be defaults for variable 2

% ... additional default initializations

if v(1)
   pair_1 = array_1;
else
   pair_1 = default_1;
end

% ... if statements for remaining pairs... 

% ... use pair_1 pair_2 ...

For instance, passing the vector

 v = [1 zeros(1,9) 1];

would indicate to the function that only the first and last parameter pairs are to be used. You should still pass 11 pairs, but the content of the pairs you do not want to use can be empty or some other value.