Default parameter if the user input is empty?

Hello, I'm quite new to MATLAB and wondered; if in a script I ask a user to enter a number as an input, how can I enter a default paramater if the user chooses not to input any number, and simply presses enter?
For exemple: Stars = input('How many pixels do you want per stars? '); if Stars == '' (this is the line of code I'm wondering about) Stars = 2000

2 Comments

I just have an additional question.
I would like my code to test whether the user input contains only a certain set of digits. If it contains digits outside of this condition, it should display an error message. How would I do this?
Aryaman Sud : is the input a single integer? For example if the purpose was to ensure the input had no 8 or 9 (because you intended to re-interpret it as octal for example) ? Is the input a vector of individual digits rather than a single number? Is the input a character vector that might have characters other than digits as well?
If the input could be floating point, then you have to be very careful about what it means for it to contain various digits. For example if the input was entered as 1.23 then you would probably think that it "obviously" contains no 9's, but you would not be correct. 1/10th cannot be represented exactly in binary floating point, the same way that 1/7 cannot be exactly represented in any fixed number of decimal digits, so when 1.23 is entered, the closest representable number would be substituted, and that would be 1.229999999999999982236431605997495353221893310546875 which contains 18 nines !

Sign in to comment.

 Accepted Answer

s = input('Enter a number\n');
if isempty(s)
s = 3;
end

3 Comments

Thank you, this was very helpful!
i wanted to know what to do if a custom function is partially empty? Like myThirdAssignment() should have 5 input parameters like myThirdAssignment('image.jpg',size,k0,k1,k2).
Now if i only want to enter the iamge file like myThirdAssignment('image.jpg') and want the default parameters for size, k0, k1,k2 and k3, how do i do that1?.
if nargin < 2 || isempty(size)
size = 512;
end
if nargin < 3 || isempty(k0)
k0 = 3;
end
if nargin < 4 || isempty(k1)
k1 = 5;
end
if nargin < 5 || isempty(k2)
k2 = 8;
end
However, we recommend against using size as the name of a variable, as size() is the name of an important MATLAB function

Sign in to comment.

More Answers (1)

I'd just like to expand this discussion.
For functions:
output = function(x,y,z)
If I type
var = function(10,20);
I'll get an error since I didn't input a z value. This can be avoided by setting a default value for z via:
if nargin == 2 % if the number of inputs equals 2
z = 5 % then make the third value, z, equal to my default value, 5.
end
Now if I only input x and y, the function will still run with a default value of z as z=5.
Hope that helps other people in the future!

8 Comments

I find it to be more maintainable to use inequalities on the tests, and it is convenient to allow the users to use [] to indicate they want the default. For example,
if nargin < 1 || isempty(x)
x = 10;
end
if nargin < 2 || isempty(y)
y = 20;
end
if nargin < 3 || isempty(z)
z = 5;
end
This makes it easy to add new parameters.
Another option is to use the inputParser. You can define required inputs, optional inputs and parameter/value pairs. This also adds the ability to add error checking for the expected data in each input.
function output = myfun(reqIn,varargin)
p = inputParser;
p.addRequired('reqIn',@numeric);
p.addOptional('optIn','default value',@ischar);
p.addParamValue('paramIn',5,@isnumeric);
p.parse(reqIn,varargin{:});
reqIn = p.Results.reqIn;
optIn = p.Results.optIn;
paramIn = p.Results.paramIn;
This can get slow though, so I only use this for functions that get called infrequently (I would resort to something like Walter's suggestion if the function gets called in loops or repetitively). It does allow you to provide default values for parameters and optional inputs.
Hi,
I am a bit confused with nargin and isempty() here.
empty does test if input is empty and nargin if input is missing, right?
But nargin does not tell which one is missing...?
If I have a function like:
function result = testNargin(first,second,third)
if nargin<1 || isempty(first)
first=1;
end
if nargin<2 || isempty(second)
second=2;
end
if nargin<3 || isempty(third)
third=3;
end
result=[first second third];
end
then:
>> testNargin(1,3)
ans =
1 3 3
Here I wanted the second input default to be 2 and I failled...
Thanks in advance for your help.
Question for you to think about with your proposal:
  • How would the user indicate that they wanted to omit the third input argument and have the second input argument set to 3?
  • How would the user indicate that they wanted to omit the second input argument and have the third input argument set to 2?
You dont need function isempty. If you have 3 arguments, you can simply do this:
if nargin<3
third=1;
end
if nargin<2
second=2;
end
if nargin<1
first=3;
end
Sorry for late respond. I've just recenlty started learning MATLAB and this helps me to understand MATLAB better.
If you want to omit an input, the MATLAB convention is to pass an empty array like []. So the call to your function would be
>> testNargin(1,[],3)
How should the function test if an argument is []?
isempty() tests for []

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!