开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜