如何在Python脚本中调用我自己的Matlab函数

时间:2013-10-02 05:13:20

标签: python matlab

我真的不喜欢Matlab的GUI和MySQL连接器,所以我尝试使用Python来代替。我已经将.m文件编译成dll(mcc -l filname.m),但我不知道知道如何调用函数和传递参数。请帮助我。

1 个答案:

答案 0 :(得分:0)

本教程解释了如何在Matlab中使用MySQL。请注意,您需要下载MySQL Connector / J JAR here(类似mysql-connector-java-5.1.25-bin.jar)。

http://www.stanford.edu/group/farmshare/cgi-bin/wiki/index.php/MatlabMysql

% Database Server
host = 'mysql-user.stanford.edu';

% Database Username/Password
user = 'gfarmsharetest';
password = 'putyourpasswordhere';

% Database Name
dbName = 'g_farmshare_testing';

% JDBC Parameters
jdbcString = sprintf('jdbc:mysql://%s/%s', host, dbName);
jdbcDriver = 'com.mysql.jdbc.Driver';

% Set this to the path to your MySQL Connector/J JAR
javaaddpath('/usr/share/java/mysql-connector-java.jar')
% On Windows it looks like: javaaddpath('C:\server\mysql-connector-java-5.1.25\mysql-connector-java-5.1.25-bin.jar')


% Create the database connection object
dbConn = database(dbName, user , password, jdbcDriver, jdbcString);

% Check to make sure that we successfully connected
if isconnection(dbConn)
    % Fetch the symbol, market cap, and last close for the 10 largest
    % market cap ETFs
    result = get(fetch(exec(dbConn, 'SELECT foo,bar from matlabfoo')), 'Data');
    disp(result);
else
    % If the connection failed, print the error message
    disp(sprintf('Connection failed: %s', dbConn.Message));
end

运行查询:

%% Running queries
% http://www.mathworks.com/help/database/run-sql-query.html

cursor = exec(connection, 'SELECT user_id FROM moocdb.users LIMIT 1000')
a = fetch(cursor)
a.Data

sql = [' SELECT observed_events.observed_event_duration ' ... 
    ' FROM moocdb.observed_events AS observed_events ' ...
    ' LIMIT 1000000000; ']
boxplot(cell2mat(a.Data))

cursor = exec(connection,sql);
a = fetch(cursor)
boxplot(cell2mat(a.Data))

% Close the connection so we don't run out of MySQL threads
close(connection);