Code covered by the BSD License  

Highlights from
combineOpt

Be the first to rate this file! 2 Downloads (last 30 days) File Size: 2.06 KB File ID: #21424

combineOpt

by Andrey Popov

 

12 Sep 2008 (Updated 06 Nov 2011)

Combine user provided option's structure with a default one

| Watch this File

File Information
Description

options = combineOpt (user_options, default_options)

The function allows easy definition and submission of options to a Matlab function.
combineOpt combines the fields of the structure user_options with the fields of the default_options's structure, during which the fields of user_options overwrite the fields of default_options.

Example:
>> user_options.B = 5;
>> user_options.C = 'foo';

>> default_options.A = 1;
>> default_options.B = 0;

>> options = combineOpt (user_options, default_options)
options =
    A: 1
    B: 5
    C: 'foo'

The function can be very useful when there are many optional arguments that can be passed to a function. A useful structuring of one's function is

function y = myfunct(x, opt)
def_options = struct('a',5,'b',0);

if nargin == 0 % return the default options
  y = def_options;
  return
else
  opt = combineOpt (opt, def_options)
end

% further code

end % function

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
15 Sep 2008 Jos x@y.z

See CATSTRUCT (http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=7842&objectType=FILE) for a similar implementation ...

Please login to add a comment or rating.
Updates
24 May 2009

Updated license in the M file

06 Nov 2011

Updated author information

06 Nov 2011

Fixed typo

Tag Activity for this File
Tag Applied By Date/Time
structures Andrey Popov 22 Oct 2008 10:19:10
cell arrays Andrey Popov 22 Oct 2008 10:19:10
function Andrey Popov 22 Oct 2008 10:19:10
options Andrey Popov 22 Oct 2008 10:19:10
default Andrey Popov 22 Oct 2008 10:19:10
structure Andrey Popov 22 Oct 2008 10:19:10
combine Andrey Popov 22 Oct 2008 10:19:10

Contact us at files@mathworks.com