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. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Import Boolean Data from Database

Import Boolean data from a database table into the MATLAB® workspace. MATLAB® imports Boolean data from databases into the MATLAB® workspace as data type logical. This data has values of true or false. You can store Boolean data in a table, structure, or cell array. Perform a simple data analysis on the imported data.

The code assumes that you have a database table Invoice stored in a Microsoft® SQL Server® database. Here, connect to a Microsoft® SQL Server® Version 11.00.2100 database using the Microsoft® SQL Server® Driver 11.00.5058.

Create Database Connection

Create a database connection to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password.

datasource = 'MS SQL Server Auth';
conn = database(datasource,'','');

Import Boolean Data

Select the paid data from the Invoice table using an SQL SELECT statement. The database stores paid data as a Boolean to specify whether or not an invoice has been paid. Import and display the data using the select function.

selectquery = 'SELECT Paid FROM Invoice';
data = select(conn,selectquery)
data =

  10×1 table



Database Toolbox™ imports the data into the workspace variable data. The MATLAB® table data contains Paid as a logical variable.

Perform Data Analysis

Count the number of unpaid invoices.

unpaid = data.Paid == false;
ans =


Close Database Connection


See Also

| |

Related Topics

External Websites