Error using Input function "The second argument to INPUT must be 's'."
13 次查看(过去 30 天)
显示 更早的评论
I have created two simple for loops that scan a two dimensional matrix for negative integers. At every value that is negative, the user is asked to enter a new value at its corresponding temperature. (column 1 of the matrix is strictly temperature values so when I ask the user to enter a new value, it will tell them at what temperature that value pertains to). I also included a while loop so that the user is constantly asked to enter new values until they enter a value that is a positive integer. However, I am getting the following error:
Error using input
The second argument to INPUT must be 's'.
Error in Project2 (line 46)
saturated_data(i,j) = input('Please replace the value at temperature %g: ',saturated_data(1,i));
Code:
for i = 1:1:r_sat
for j = 2:1:c_sat
while saturated_data(i,j) < 0
saturated_data(i,j) = input('Please replace the value at temperature %g: ',saturated_data(i,1));
end
end
end
If someone could please tell me how I can fix this error, I would greatly appreciate it. I think it has something to do with the statement changing at every iteration.
0 个评论
采纳的回答
Cris LaPierre
2018-11-14
编辑:Cris LaPierre
2018-11-14
The error message is telling you exactly what the issue is, and the documenation will show you what the proper syntax should be:
Syntax
x = input(prompt)
str = input(prompt,'s')
So if you are calling input with two arguments, the second must be 's'.
In your case, your prompt is being treated as the two separate arguments. Best would be to build the prompt message first, and then pass it in as a string or char array. An example from the doc page:
prompt = 'Do you want more? Y/N [Y]: ';
str = input(prompt,'s');
4 个评论
DGM
2022-6-5
If you're indeed having the same error, then the answer you're commenting on explains the error and gives an example of how to fix it.
Steven Lord
2022-6-5
The input function does not accept format strings and values like the sprintf and fprintf functions do. Build the prompt string before you call input or inside the input call using sprintf.
value = 42;
promptString = sprintf("Enter the value for x = %d: ", value);
Y = input(promptString)
% or if you want text
name = input(sprintf("Enter the name of player %d: ", value), 's')
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!