Borrar filtros
Borrar filtros

Error using Input function "The second argument to INPUT must be 's'."

16 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 14 de Nov. de 2018
Editada: Cris LaPierre el 14 de Nov. de 2018
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.
  4 comentarios
DGM
DGM el 5 de Jun. de 2022
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
Steven Lord el 5 de Jun. de 2022
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')

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by