How to prompt for input using an existing variable in the prompt
I'm trying to ask the user for a value of some variable, but at the same time, showing him the last value used (at the termination of the program, the values are saved to a file, and loaded at the start of the program).
Something like this:
Enter new radius value (R=12.6) :
... user enters 12.7 ... Enter new pi value (pi=3.14) : Enter new height value (h=30.0) :Usually I would write the first one with write statement, then read the new one (in Fortran, for example). In MATLAB however, I don't know how to write something out with input statement. Are there some other statem开发者_开发知识库ents for getting input ?
You can use the command input
for this, combined with sprintf
.
%# set defaults
radius = 12.6;
%# ask for inputs
tmp = input(sprintf('Enter new radius value (R=%4.2f)\n',radius));
%# if the user hits 'return' without writing anything, tmp is empty and the default is used
if ~isempty(tmp)
radius = tmp;
end
As an alternative, you may want to look into INPUTDLG
精彩评论