Default parameter if the user input is empty?

141 次查看(过去 30 天)
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 个评论
Aryaman Sud
Aryaman Sud 2020-4-1
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
Walter Roberson 2020-4-1
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 !

请先登录,再进行评论。

采纳的回答

Wayne King
Wayne King 2011-11-11
s = input('Enter a number\n');
if isempty(s)
s = 3;
end
  3 个评论
Abdul Raffay
Abdul Raffay 2020-7-8
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
Walter Roberson 2020-7-8
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

请先登录,再进行评论。

更多回答(1 个)

Seth Heerschap
Seth Heerschap 2016-5-19
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 个评论
Real User
Real User 2021-5-28
How should the function test if an argument is []?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by