How to declare variables in a function

7 visualizaciones (últimos 30 días)
Blue
Blue el 1 de Ag. de 2019
Comentada: Adam Danz el 1 de Ag. de 2019
Hi,
I am curious as to why the variables 'user' and 'pass' are not recognized by Matlab in the following snippet. When the user launches the function, Matlab throws a undefined function or variable 'pben' where pben is the input from the user.
function update_table(user, pass)
% Info
username = user;
password = pass;
datasource = 'SGDOP';
% Connection and SQL query
conn = database(datasource, username, password);
sqlquery = ['SELECT * from table x'];
data = fetch(conn, sqlquery);
save('data.mat', 'data')
end
  4 comentarios
Blue
Blue el 1 de Ag. de 2019
Hi,
I am launching the function as update_tabel(pben, aaaa) where pben is the user and aaaa would be the password. Then the user and password are used to establish the connection to a database.
Adam Danz
Adam Danz el 1 de Ag. de 2019
My bet is that when you call update_tabel(pben, aaaa), pben is not defined.

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 1 de Ag. de 2019
My guess is that you're trying to call this function like:
update_table(pben, somePassword)
That attempts to pass the contents of the variable named pben or the result of calling a function named pben with 0 input arguments and 1 output argument into update_table. If you want to pass the literal text pben into the function, you need to specify it as a char vector or as a string.
update_table('pben', somePassword)
update_table("pben", somePassword)
If your password is not stored in a variable named somePassword but is the literal text somePassword, you need to pass that in as text as well.
update_table('pben', 'somePassword')
update_table("pben", "somePassword")
  1 comentario
Blue
Blue el 1 de Ag. de 2019
Ah. That was embarrasingly simple. Thank you.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by