Default parameter if the user input is empty?
Ältere Kommentare anzeigen
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 Kommentare
Aryaman Sud
am 1 Apr. 2020
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?
Walter Roberson
am 1 Apr. 2020
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 !
Akzeptierte Antwort
Weitere Antworten (1)
Seth Heerschap
am 19 Mai 2016
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 Kommentare
Walter Roberson
am 19 Mai 2016
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.
Matt Butts
am 19 Mai 2016
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.
Laurent Davenne
am 24 Nov. 2017
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.
Walter Roberson
am 25 Nov. 2017
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?
Faris Hajdarpasic
am 20 Feb. 2019
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.
Ameya Deoras
am 4 Mär. 2019
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)
Real User
am 28 Mai 2021
How should the function test if an argument is []?
Walter Roberson
am 28 Mai 2021
isempty() tests for []
Kategorien
Mehr zu Variables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!