poster
2019-12-10, 20:41
我正在尝试向用户询问某个变量的值,但同时,向用户显示最后使用的值(在程序终止时,这些值将保存到文件中,并在开始时加载程序)。
像这样:
输入新的半径值(R = 12.6):
...用户输入12.7 ...
输入新的pi值(pi = 3.14):
输入新的高度值(h = 30.0):
通常,我会使用write语句编写第一个,然后读取新语句(例如,在Fortran中)。但是在MATLAB中,我不知道如何用输入语句写一些东西。还有其他获取输入的语句吗?
回答:
您可以将命令input与此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 或者,您可能需要研究INPUTDLG (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/inputdlg.html)
更多&回答... (https://stackoverflow.com/questions/2794125)
像这样:
输入新的半径值(R = 12.6):
...用户输入12.7 ...
输入新的pi值(pi = 3.14):
输入新的高度值(h = 30.0):
通常,我会使用write语句编写第一个,然后读取新语句(例如,在Fortran中)。但是在MATLAB中,我不知道如何用输入语句写一些东西。还有其他获取输入的语句吗?
回答:
您可以将命令input与此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 或者,您可能需要研究INPUTDLG (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/inputdlg.html)
更多&回答... (https://stackoverflow.com/questions/2794125)