Default parameter if the user input is empty?
135 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Maxime
am 11 Nov. 2011
Kommentiert: Walter Roberson
am 28 Mai 2021
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
Wayne King
am 11 Nov. 2011
s = input('Enter a number\n');
if isempty(s)
s = 3;
end
3 Kommentare
Abdul Raffay
am 8 Jul. 2020
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?.
Walter Roberson
am 8 Jul. 2020
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
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
Siehe auch
Kategorien
Mehr zu Performance and Memory finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!