Set default values of input variables
default is useful for setting the default value of an optional value.
For example,
a function
function c=add(a,b)
default('a',1);
default('b',2);
c=a+b;
gives the following results:
>> add()
ans =
3
>> add(2)
ans =
4
>> add(2,5)
ans =
7
default can assign ordinary variables, or it can assign fields of structs.
default counts empty variables as unset, and so will assign them a default value, unless the opposite behavior is specified with count_empty=0.
For example,
>> add([],4)
ans =
5
allows us to default the first input while specifying the second.
default works by executing 'isempty' and 'exist' in the caller workspace using the 'evalin' command. It uses 'assignin' to assign the value.
Cite As
Joe Eichholz (2024). Set default values of input variables (https://www.mathworks.com/matlabcentral/fileexchange/59853-set-default-values-of-input-variables), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
- MATLAB > Programming > Functions > Variables >
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.