Default parameter if the user input is empty?

조회 수: 135 (최근 30일)
Maxime
Maxime 2011년 11월 11일
댓글: Walter Roberson 2021년 5월 28일
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 []?
Walter Roberson
Walter Roberson 2021년 5월 28일
isempty() tests for []

댓글을 달려면 로그인하십시오.

카테고리

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