Hello all, I have declare a variable "ax" as global but this variable cannot fetch by SQL query and says "Undefined function or variable 'ax'". So how can we fetch this variable. Please give solution. thanks in advance.

curs = exec(conn, ['SELECT pass.`ch&n`'...
' FROM pass '...
'WHERE pass.xaxis = ' '''' ax '''']);
curs = fetch(curs);
curs.data

Answers (2)

Bhupendra - the error message is telling you exactly what the problem is. The ax variable has not been defined prior to you trying to use it. Since you are using this as a condition in your where clause, you would need to do something like
ax = '42';
curs = exec(conn, ['SELECT pass.`ch&n`'...
' FROM pass '...
' WHERE pass.xaxis = ' '''' ax '''']);
curs = fetch(curs);
curs.data
Note that the above is just an example. I don't know what your ax should be so you will have to set this variable to the appropriate value.
Or, since you have declared this variable as global (which can often lead to problems like this one), you would need to declare it as global before trying to reference it. In your function or script you would do
global ax;
curs = exec(conn, ['SELECT pass.`ch&n`'...
' FROM pass '...
' WHERE pass.xaxis = ' '''' ax '''']);
curs = fetch(curs);
curs.data

1 Comment

I would firmly recommend constructing the command using sprintf() and pass that to exec.
cmd = sprintf('SELECT pass.`ch&n FROM pass WHERE pass.xaxis = '%d', ax);
curs = exec(conn, cmd);
I am guessing here at the data type of ax.
And remember...

Sign in to comment.

I think it likely that you are assigning to ax in one place and then assuming that you can just use it in the callback. However, variable are local to their workspace unless you specifically tell MATLAB otherwise. See

Categories

Asked:

on 8 Jul 2016

Answered:

on 13 Jul 2016

Community Treasure Hunt

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

Start Hunting!