Why do I need to use different syntax to access the results of my SQL query with Database Toolbox 3.2 (R2006b) than with earlier versions?

1 view (last 30 days)
I have the following code to retrieve data from my database
setdbprefs('DataReturnFormat','cellarray');
curs = exec(conn, sql);
curs=fetch(curs);
clprice=[curs.Data{:,2}];
With Database Toolbox 3.2 (R2006b), this last statement returns only one element although I have multiple elements in curs.Data as verified by the following output
K>> curs.Data
ans =
'A-NBP-GasY05' [ 4.7031e+009]
'A-NBP-GasY06' [ 5.4298e+009]
'A-NBP-GasY07' [ 5.3461e+009]
'A-NBP-GasY08' [ 4.8868e+009]
'A-NBP-Sm06' [ 3.3269e+009]
'A-NBP-Sm07' [ 4.3463e+009]
'A-NBP-Sm08' [ 4.2863e+009]
'A-NBP-Wn06-07' [ 6.5192e+009]
The same line of code would return the entire first column in earlier versions of Database Toolbox.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This change in behavior is due to an incompatibility introduced in MATLAB 7.3 (R2006b). This change was introduced to fix a bug related to indexing in cell arrays.
The change is that when "a" is a cell array, x = a{:,1} no longer throws an error but returns the first element of the output. This is now the desired behavior both in Database Toolbox and MATLAB.
If you wish to retrieve a column of data as a matrix, use the CELL2MAT function when all of the data in the Data field is numeric.
For example , instead of typing
[curs.Data{:,1}]
use
cell2mat(curs.Data(:,1))
Also, when using the Data property of a cursor object, it is not possible to return multiple outputs when using the cell indexing notation {}.
For example:
[a,b]=curs.Data{:,2}
returns the error message
??? Error using ==> cursor.subsref
Too many output arguments.
If you with to return multiple outputs from the Data cell array, you will need to create a temporary copy of the Data field and then perform the indexing.
x=curs.Data;
[a,b]=x{:,1}
This will display the output
a =
1
b =
2
For more information on the bug that was fixed see the following Bug Report http://www.mathworks.com/support/bugreports/details.html?rp=301697

More Answers (0)

Products


Release

R2006b

Community Treasure Hunt

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

Start Hunting!