Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Visual Query Builder

Asked by George on 1 Oct 2011

Hi i want to retrieve sql data using matlab.

The problem is that i want to use in the "where" statement a matlab variable.

Something like this:

e = exec(conn,'SELECT COLUMN1,COLUMN2 FROM DATABASE_NAME WHERE COLUMN1 IN (MATLAB_VARIABLE);

But there is an error: "Invalid Column Name".

I think that doesn't recognise that "MATLAB_VARIABLE" is a matlab variable.

If i try this: e = exec(conn,'SELECT COLUMN1,COLUMN2 FROM DATABASE_NAME WHERE COLUMN1 IN (MATLAB_VARIABLE(1)); again the following error occus:"MATLAB_VARIABLE" is not a recognised built in funtion name

Any help would be appreciated

Thanks George

0 Comments

George

Products

No products are associated with this question.

1 Answer

Answer by Oleg Komarov on 1 Oct 2011
Accepted answer

You have to convert the content of matlab variable to a char variable.

If MATLAB_VARIABLE is already a char then simply:

str = ['SELECT COLUMN1,COLUMN2 FROM DATABASE_NAME WHERE COLUMN1 IN (' MATLAB_VARIABLE(1) ')'];
exec(conn, str)

Otherways, if it is a scalar double you have to use sprintf or num2str or str2double.

In case you have an array of values you have to use sprintf and interveal with commas.

1 Comment

George on 1 Oct 2011

Thank you very much. I follow the instructions from the link: http://www.mathworks.com/support/solutions/en/data/1-174JK/index.html?product=DB&solution=1-174JK

Oleg Komarov

Contact us