Home >Database >Mysql Tutorial >How to read database Mysql file with MATLAB?
When processing large amounts of data, it is more convenient and faster to use MATLAB to directly read the database files. Here is a method to link MATLAB to the database.
1. Here we mainly explain how to connect the database using ODBC. Here we take MySQL as an example.
First of all, you must configure the MySQL data source of DDBC
2. After configuring the data source, you need to establish the connection object-database
Calling format:
conna=database(‘datasourcename’,’username’,’password’);
By default, the username and password of the database file are empty
mysql efun is the name of the data source created above
3. Create and open the cursor - exec
Calling format:
curs=exec(conna,'sqlquery')
sqlquery is used when querying database data SQL statement, special reminder, when there are variables in the query statement, remember to create another char statement, and then query again, as in the following example
4. For example, if you want to query the order information of those whose names are matrix a, when there are many names or need to be read from other documents, you can use the above example to store the information that needs to be read in a variable. Then use the strcat function to connect the variable and the query statement into a sentence, then assign it to d as a character, and finally put it in the exec cursor for query. The following figure is the wrong approach.
5. Read the data in the database into Matlab - fetch
Calling format:
curs=fetch(curs,RowLimit);
RowLimit is the number of rows of data parameters read each time. If not filled in, it will default to reading all
Then the read data is assigned to the variable to be calculated
6. Develop good habits and close link objects and cursors at will - close
When the database link object is no longer used It should be closed in time so that the memory can be released in time.
#7. Finally, I will introduce to you some commonly used functions to view data-related information
Rows-View the number of data rows
Calling format: numrows=rows(curs)
Cols——View the number of data columns
Calling format: numcols=cols(curs)
Attr— —View data attributes
Calling format: attribute=attr(curs)
The above is the detailed content of How to read database Mysql file with MATLAB?. For more information, please follow other related articles on the PHP Chinese website!