This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.


Retrieve number of columns in fetched data set


numcols = cols(curs)



numcols = cols(curs) returns the number of columns in the fetched data set curs.


collapse all

Create a database connection conn using the dbdemo data source.

conn = database('dbdemo','','');

Working with the dbdemo data source, use fetch to import all data into Database Cursor Object curs. Store the data in a cell array contained in the cursor object field curs.Data.

curs = exec(conn,'SELECT * FROM productTable');
curs = fetch(curs);

View the contents of the Data property in the cursor object.

ans = 

    [ 9]    [125970]    [1003]    [13]    'Victorian Doll' 
    [ 8]    [212569]    [1001]    [ 5]    'Train Set'      
    [ 7]    [389123]    [1007]    [16]    'Engine Kit'     
    [ 2]    [400314]    [1002]    [ 9]    'Painting Set'   
    [ 4]    [400339]    [1008]    [21]    'Space Cruiser'  
    [ 1]    [400345]    [1001]    [14]    'Building Blocks'
    [ 5]    [400455]    [1005]    [ 3]    'Tin Soldier'    
    [ 6]    [400876]    [1004]    [ 8]    'Sail Boat'      
    [ 3]    [400999]    [1009]    [17]    'Slinky'         
    [10]    [888652]    [1006]    [24]    'Teddy Bear'     

Data contains the productTable data.

Display the number of columns in the Data property in the cursor object.

numcols = cols(curs)
numcols =


The data in the cursor object contains five columns.

After you finish working with the cursor object, close it.


Close the database connection.


Input Arguments

collapse all

Database cursor, specified as a cursor object created using the exec function.

Output Arguments

collapse all

Number of columns in a data set, returned as a numeric scalar.

Introduced before R2006a

Was this topic helpful?