user2023068
user2023068

Reputation: 445

Why am I getting a Cell array and not a Table in Matlab - SQL query and database connection?

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!

Upvotes: 0

Views: 259

Answers (1)

UJIN
UJIN

Reputation: 1758

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');

Upvotes: 2

Related Questions