0

I am connecting to a database in Matlab and doing a SQL query on the database. The issue I have is why the type being returned is a cell array and not a table. The code is below, I've omitted the specific details of my database.

% Clear the MATLAB worksapce 
clear
clc

% Run SQL Script
% Create an ODBC database connection to a Microsoft(R) SQL Server(R)
% database with Windows(R) authentication. Specify a blank user name and
% password.
% Selecting the database with the default datasource as "SQLMiniProject"
datasource = 'my_project';
username = 'username';
password = 'password';

%Connecting to the database
conn = database(datasource, username,password);

% files for queries 
test_script = 'sql_test_script.sql';


results= runsqlscript(conn,'sql_test_script.sql');

close(conn);

What I am getting back from the above code is ...

Data: {15×2 cell}
     RowLimit: 0
     SQLQuery: 'select FIRST_NAME AS 'FirstName', LAST_NAME AS 'LastName'  from TABLE_1'
      Message: []
         Type: 'ODBCCursor Object'
    Statement: [1×1 database.internal.ODBCStatementHandle]

The Data is being returned as a cell and not a Table, which I would expect. Does anyone have any guidance on this?

Many thanks in advance!

3
  • 1
    You can try to use setdbprefs('DataReturnFormat','table'). I am not posting it as an answer because I have no real experience with database handling in MATLAB. It is weird though because this should be the default output type! Commented Mar 13, 2017 at 16:22
  • Thank you. That did exactly what I needed! Commented Mar 13, 2017 at 16:45
  • So, I'll write it as an answer for future reference :) Commented Mar 13, 2017 at 16:49

1 Answer 1

2

You can specify the output type by calling setdbprefs and specifying either cell or table. In your case you need to call:

setdbprefs('DataReturnFormat', 'table');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.