Pre-specify user input to function

조회 수: 9 (최근 30일)
Scott Morgan
Scott Morgan 2016년 8월 5일
답변: Steven Lord 2016년 8월 5일
This is the first time I've posted here, so apologies if it's in the wrong place.
I have a function which takes a user input and I would like to be able to pre-specify the input before the function is called. A basic example is below
function c = myfunc(a,b)
d = input('Enter value for d: ');
c = a + b + d;
end
My function is called inside a loop and I would like to not have to manually enter my 'd' value each time. I also cannot add d to the list of function arguments, because the function is called in lots of other places in the code and I don't want to go through and change them all.
Essentially, I would like the functionality that this bash script (script.sh) has
#!/bin/bash
echo "Specify d: "
read d
echo $d
when called as
./script.sh <<< 5
Is this possible?
Thanks.

채택된 답변

Stephen23
Stephen23 2016년 8월 5일
편집: Stephen23 2016년 8월 5일
You could use a persistent variable:
function c = myfunc(a,b)
persistent d
if isempty(d)
d = input('Enter value for d: ');
end
c = a + b + d;
end
and calling:
>> myfunc(1,2)
Enter value for d: 7
ans =
10
>> myfunc(1,3)
ans =
11
>> myfunc(1,4)
ans =
12
To reset the d value could either add some code (e.g. set d=[] when the function is called with no inputs) or use clear:
>> clear myfunc
>> myfunc(1,5)
Enter value for d: 3
ans =
9
>> myfunc(1,6)
ans =
10
  댓글 수: 1
Scott Morgan
Scott Morgan 2016년 8월 5일
Thanks for the quick reply! This is exactly what I wanted.

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

추가 답변 (2개)

Guillaume
Guillaume 2016년 8월 5일
Yes, this is absolutely the right place.
No, it is not possible to programmatically feed an input to the input function.
I don't get the "the function is called in lots of other places in the code and I don't want to go through and change them all.". Using your hypothetical syntax, you would still have to modify each call to prefeed the value.
If you only want to manually input a value once, and reuse the same value thereafter, you can modify the function to use a persistent variable:
function c = myfunc(a,b)
persistent d;
if isempty(d) %only prompt on first call
d = input('Enter value for d: ');
end
c = a + b + d;
end
Note: to reinitialise the persistent variable to empty:
clear myfunc

Steven Lord
Steven Lord 2016년 8월 5일
I would use nargin or separate the computational piece from the interface piece.
nargin based approach
function c = myfunc(a, b, d)
if nargin < 3
d = input('Enter value for d: ');
end
c = a + b + d;
end
separation of concerns approach
function c = myfunc(a,b)
d = input('Enter value for d: ');
c = myfuncImpl(a, b, d);
end
Those places in your code where you need to prompt the user for a value of d would call myfunc.
function c = myfuncImpl(a, b, d)
c = a + b + d;
end
Those places for which d is already known and the code does not need to prompt the user would call myfuncImpl instead of myfunc.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by