Keeping Some MATLAB Function Input Arguments Inactive

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.

2 个评论

How about using a structure instead of so many individual elements--keeping track of that many individual arguments has got to be a real pita both in writing code and particularly in trying to use any such function in the end.
As for "ignoring" any inputs, you have to write the logic to only use what is there and if an argument is expected by being in the argument list then that's what you've told Matlab to expect so there isn't any other option. You can, of course, assign default values and use those but again it has to be coded.
You didn't show any code to see why the error--the vargin argument is just a cell array containing the optional arguments so if you tried to use some fixed index as a positional indication of which one is/isn't there then you could easily have been outside the range of those that were passed. That's what the value nargin tells you--how many actually were passed; you can't access more than that. But, while that tells you how many were passed you don't know what they're supposed to be unless you have some identification of them.
That's why the named structure would work or why/how the lists of properties/values in such functions as plot work--see the
doc varargin
for example of that syntax/description of how they're used.
Thank you for your reply.
This is 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;
I agree with what you wrote in the 2nd and 3rd paragraphs of your answer. The problem is that 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, for example. I hope that clarifies my problem to some extent.

请先登录,再进行评论。

 采纳的回答

OK, I reformatted the above and shortened names down to not be so much obfuscation by excess amounts of superfluous characters...
I'll go ahead and take a guess...and a shot at one rather crude solution--I think one could probably vectorize the whole thing w/ some thought...
First, instead of 22 individual variables, use two (or even one 2D) array...
function {Ep, ...] = supdem(data, ..., p, ip)
ix=~isempty(p); % which to use
p(isempty(p))=1; % a dummy to keep from erroring
ip(isempty(ip))=1; % a dummy to keep from erroring
Eprout = (ix(1)* sum(sum(data(p(1), ip(1))))+ ...
ix(2)* sum(sum(data(p(2), ip(2))))+ ...
ix(3)* sum(sum(data(p(3), ip(3))))+ ...
...
ix(10)*sum(sum(data(p(10),ip(10))))+ ...
ix(11)*sum(sum(data(p(11),ip(11))))/1000;
Alternatively, use try...catch blocks.
OBTW, need to check for consistency between the two or deal with the interaction between the two by having two vectors.
all(isempty(p)==isempty(ip))

2 个评论

You're right - a logical array solved my problem. Thank you for your help :-)

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by