Using a variable in an input prompt

190 ビュー (過去 30 日間)
lsutiger1
lsutiger1 2015 年 11 月 6 日
回答済み: Steven Lord 2024 年 9 月 28 日
I am trying to use the iterator (ii) of my for loop as a string in my input prompt. The code is shown below
for ii = 1:n
x = input('What is the orientation of molecule ' num2str(ii) 'in the x-direction?');
end
but this does not work. Any suggestions?
  1 件のコメント
lsutiger1
lsutiger1 2015 年 11 月 6 日
Thank you to all who replied! Much appreciated!!

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2015 年 11 月 6 日
x = input(['What is the orientation of molecule ' num2str(ii) 'in the x-direction?']);
  1 件のコメント
lsutiger1
lsutiger1 2015 年 11 月 6 日
Thank you!

サインインしてコメントする。

その他の回答 (2 件)

Wanbin Song
Wanbin Song 2015 年 11 月 6 日
Input argument of 'input' function should be a text.
Your code shows that your input arguments of 'input' function consists of three string.
Just combine them as below.
for ii = 1:n
x = input(['What is the orientation of molecule ' num2str(ii) 'in the x-direction?']);
end
Then it will work.
  1 件のコメント
lsutiger1
lsutiger1 2015 年 11 月 6 日
Thank you!

サインインしてコメントする。


Steven Lord
Steven Lord 2024 年 9 月 28 日
Another possibility you can use in recent releases of MATLAB (since I think release R2016a) is to use a string array.
ii = 5;
promptMessage = "What is the orientation of molecule " + ii + " in the x direction?"
promptMessage = "What is the orientation of molecule 5 in the x direction?"
You could then pass promptMessage into the input function (or directly construct the prompt message inside the input call.) I didn't show that because calling input is not supported on MATLAB Answers.

Community Treasure Hunt

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

Start Hunting!

Translated by