setdbprefs
(Not recommended) Set preferences for retrieval format, errors, NULLs, and more
The setdbprefs
function is not recommended. For details about
functionality to use instead, see Version History.
Description
setdbprefs
returns current values for database
preferences.
setdbprefs(
returns the
current value for the specified preference
)preference
.
setdbprefs(
sets the specified preference
,value
)preference
to value
.
After you set database preferences, they are retained across MATLAB® sessions.
Examples
Display Current Values
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'
Change Preference Setting
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 to Structure
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'
Return Values to Structure
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 Preferences
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
preference
— Database preference
character vector | cell array
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 thedatabase
function. To specify displaying errors in the Command Window, entersetdbprefs('ErrorHandling','report')
. Otherwise, you can access the error message in theMessage
property of theconnection
object.NULL
data — Specify how to importNULL
strings into the MATLAB workspace. To importNULL
strings as the character vector'null'
, entersetdbprefs('NullStringRead','null')
. Set this parameter before runningfetch
.
Example: 'ErrorHandling'
Example: {'ErrorHandling';'NullStringRead'}
Data Types: char
value
— Database preference value
character vector | cell array
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
s
— Database preferences
structure
Database preferences, specified as a structure that includes all the preferences you specify.
Data Types: struct
Output Arguments
v
— Database preferences
structure
Database preferences, returned as a structure containing database preference settings and values.
Version History
Introduced before R2006aR2019a: setdbprefs
function is not recommended
The setdbprefs
function is not recommended. Use the following
replacement functionality to specify the data return format, error handling, and
missing data. Some differences between the workflows might require updates to your
code.
Data return format — For the
'DataReturnFormat'
database preference, these values are not recommended:'numeric'
'cellarray'
'structure'
Error handling — The
'ErrorHandling'
database preference is not recommended.Missing data — The
'NullNumberWrite'
,'NullStringWrite'
, and'NullNumberRead'
database preferences for handlingNULL
data values are not recommended.
There are no plans to remove the setdbprefs
function at this
time.
To set the data return format in prior releases, you specified returning
imported data as a numeric matrix by setting the
'DataReturnFormat'
database preference to the value
'numeric'
. For example:
setdbprefs('DataReturnFormat','numeric') results = fetch(conn,sqlquery);
Now you can set the same value by using the 'DataReturnFormat'
name-value pair argument of the fetch
function.
results = fetch(conn,sqlquery,'DataReturnFormat','numeric');
Or, you can customize import options.
opts = databaseImportOptions(conn,tablename); varnames = "quantity"; opts = setoptions(opts,varnames,'Type','int64');
To specify error handling in prior releases, you set the
'ErrorHandling'
database preference to the value
'report'
or 'store'
by using the
setdbprefs
function. For example:
setdbprefs('ErrorHandling','store')
Now you specify error handling by using the 'ErrorHandling'
name-value pair argument of the database
function or the
'ErrorHandling'
name-value pair argument of the executeSQLScript
function.
conn = database(datasource,username,password,'ErrorHandling','store');
To specify the handling of missing data in prior releases, you set the
'NullNumberWrite'
database preference to a specific
value, for example. This table shows database preference settings that are not
recommended and the functionality you can use instead.
Discouraged Functionality | Recommended Replacement |
---|---|
setdbprefs('NullNumberWrite', | |
setdbprefs('NullStringWrite', | |
setdbprefs('NullNumberRead', | SQLImportOptions object |
See Also
clear
| fetch
| getdatasources
| database
| close
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)