Products & Services Solutions Academia Support User Community Company

Learn more about Database Toolbox   

Importing and Exporting BOOLEAN Data

Importing BOOLEAN Data from Databases to the MATLAB Workspace

BOOLEAN data is imported from databases into the MATLAB workspace as data type logical. This data has a value of 0 (false) or 1 (true), and is stored in a cell array or structure.

This example imports data from the Products table in the Nwind database into the MATLAB workspace.

  1. Set Data return format to cellarray.

  2. For Data operation, choose Select.

  3. In Data source, select SampleDB.

  4. In Tables, select Products.

  5. In Fields, select ProductName and Discontinued.

  6. Assign the query results to the MATLAB workspace variable D.

  7. Click Execute to run the query.

    VQB retrieves a 77-by-2 array.

  8. Enter D in the MATLAB Command Window. 77 records are returned; only the first five records appear here due to space constraints.

    D = 
        'Chai'                   [0]
        'Chang'                  [0]
        'Aniseed Syrup'          [0]
                  [1x28 char]    [0]
                  [1x22 char]    [1]
    
  9. Compare these results to the data in Microsoft Access.

  10. In the VQB Data area, double-click D to view its contents in the Variable Editor. The logical value for the first product, Chai, appears as false instead of 0.

  11. In the Variable Editor, double-click false. Its logical value, 0, appears in a separate window.

For more information about MATLAB logical data types, see Logical Types in the MATLAB Programming Fundamentals documentation.

Exporting BOOLEAN Data from the MATLAB Workspace to Databases

Logical data is exported from the MATLAB workspace to a database as type BOOLEAN. This example adds two rows of data to the Products table in the Nwind database.

  1. In the MATLAB workspace, create P, the structure you want to export.

    P.ProductName{1,1}='Chocolate Truffles';
    P.Discontinued{1,1}=logical(0);
    P.ProductName{2,1}='Guatemalan Coffee';
    P.Discontinued{2,1}=logical(1);
    
  2. For Data operation, choose Insert.

  3. In Data source, select SampleDB.

  4. In Tables, select Products.

  5. In Fields, select ProductName and Discontinued.

  6. Assign results to the MATLAB workspace variable P.

  7. Click Execute to run the query.

    VQB inserts two new rows into the Products table.

View the table in Microsoft Access to verify that the data was correctly inserted.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS