Azzera filtri
Azzera filtri

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

16 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Cris LaPierre
Cris LaPierre il 14 Nov 2018
Modificato: Cris LaPierre il 14 Nov 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 Commenti
DGM
DGM il 5 Giu 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 il 5 Giu 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')

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by