MATLAB: Error using Input function “The second argument to INPUT must be ‘s’.”

for loopinputwhile loop

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.

Best Answer

A great place to look at examples is the documentation for input.
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');
To populate your prompt string with a variable value, look into using sprintf.