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.

setdbprefs

Set preferences for retrieval format, errors, NULLs, and more

Syntax

setdbprefs
v = setdbprefs
setdbprefs(preference)
setdbprefs(preference,value)
setdbprefs(s)

Description

example

setdbprefs returns current values for database preferences.

example

v = setdbprefs returns current values to the structure v.

example

setdbprefs(preference) returns the current value for the specified preference.

example

setdbprefs(preference,value) sets the specified preference to value. After you set database preferences, they are retained across MATLAB® sessions.

example

setdbprefs(s) sets preferences specified in the structure s to values that you specify.

Examples

collapse all

Display all database preferences and their current values.

setdbprefs
ans = 

  struct with fields:

    DataReturnFormat: 'table'
       ErrorHandling: 'store'
      NullNumberRead: 'NaN'
     NullNumberWrite: 'NaN'
      NullStringRead: 'null'
     NullStringWrite: 'null'

Display the current value for the specified database preference.

setdbprefs('ErrorHandling')
ans =

    'report'

Set a database preference to a different value. Change the display of errors in MATLAB by modifying the database error handling preference.

Specify the store format for the ErrorHandling preference.

setdbprefs('ErrorHandling','store')

When you execute the database function, Database Toolbox™ stores any generated errors in the Message property of the returned connection object.

Establish the connection conn to a MySQL® database with the user name username and an invalid password.

conn = database('MySQL','username','invalid');

Access the error message in the Message property of the connection object.

conn.Message
ans =

    'ODBC Driver Error: [MySQL][ODBC 5.3(a) Driver]Access denied for user 'username'@'servername' (using password: YES)'

Specify the report format for the ErrorHandling preference.

setdbprefs('ErrorHandling','report')

Connect to the database using the invalid password again. With the ErrorHandling preference set to report, the error generated by running the database function appears immediately in the Command Window.

conn = database('MySQL','username','invalid')
Error using database (line 156)
ODBC Driver Error: [MySQL][ODBC 5.3(a) Driver]Access denied for user
'username'@'servername' (using password: YES)

Assign values for specific preferences in a structure so you can change multiple database preferences simultaneously.

Assign values for preferences to fields in the structure s.

s.ErrorHandling = 'report'; 
s.NullStringRead = 'null';
s
s = 

  struct with fields:

       ErrorHandling: 'report'
      NullStringRead: 'null'

Set preferences using the values in s.

setdbprefs(s)

Run setdbprefs to check your database preference settings.

setdbprefs
ans = 

  struct with fields:

    DataReturnFormat: 'table'
       ErrorHandling: 'report'
      NullNumberRead: 'NaN'
     NullNumberWrite: 'NaN'
      NullStringRead: 'null'
     NullStringWrite: 'null'

Assign values for all database preferences to s.

s = setdbprefs
s = 

  struct with fields:

    DataReturnFormat: 'table'
       ErrorHandling: 'report'
      NullNumberRead: 'NaN'
     NullNumberWrite: 'NaN'
      NullStringRead: 'null'
     NullStringWrite: 'null'
      

Save your database preferences to the MAT-file to use them in future MATLAB sessions.

Assign the preferences to the variable ImportData and save them to a MAT-file ImportDataPrefs in your current folder.

ImportData = setdbprefs;
save ImportDataPrefs.mat ImportData

Load the data and restore the preferences.

load ImportDataPrefs.mat
setdbprefs(ImportData)

Input Arguments

collapse all

Database preference, specified as a character vector or cell array. To set multiple database preferences, enter the preference values in a cell array of character vectors. Then, match the order with the corresponding values in the value argument.

You can specify database preferences for error handling and importing NULL strings from a database into MATLAB.

  • 'ErrorHandling' — Specify how to handle errors when importing data. Set this parameter before you execute the database function. To specify displaying errors in the Command Window, enter setdbprefs('ErrorHandling','report'). Otherwise, you can access the error message in the Message property of the connection object.

  • NULL data — Specify how to import NULL strings into the MATLAB workspace. To import NULL strings as the character vector 'null', enter setdbprefs('NullStringRead','null'). Set this parameter before running fetch.

Example: 'ErrorHandling'

Example: {'ErrorHandling';'NullStringRead'}

Data Types: char

Database preference value, specified as a character vector or cell array. To set multiple database preferences, enter the preference values in a cell array of character vectors. Then, match the order with the corresponding preferences in the preference argument.

Example: 'NaN'

Example: {'numeric';'NaN'}

Data Types: char

Database preferences, specified as a structure that includes all the preferences you specify.

Data Types: struct

Output Arguments

collapse all

Database preferences, returned as a structure containing database preference settings and values.

Alternative Functionality

For a visual way to set database preferences, click Preferences in the Environment section of the MATLAB toolstrip, and then click Database Toolbox. Enter values for each database preference.

Compatibility Considerations

expand all

Not recommended starting in R2018b

Not recommended starting in R2018b

Not recommended starting in R2018b

Introduced before R2006a