python调用matlab脚本

一、MATLAB 安装部署
软件智库-Matlab 2018a中文版软件下载和安装教程
博客园-MATLAB R2019b超详细安装教程(附完整安装文件)
本人使用的第二种。
二、Python调用Matlab方法以及测试程序
1、python测试代码

import matlab.engine import pymysql print("start matlab engine...") eng = matlab.engine.start_matlab() print("matlab engine is start!")db = pymysql.connect("127.0.0.1", "root", "123456", "test", charset='utf8') cursor = db.cursor() sql = "SELECT * FROM usertb" try: cursor.execute(sql) results = cursor.fetchall() for row in results: y = eng.calc_area(row[0],row[1]) print(y) except: print "Error: unable to fecth data"db.close()

2、数据库 可以不用数据库,直接在python中调用matlab函数即可!
-- -- 表的结构 `usertb` --CREATE TABLE `usertb` ( `id` bigint(20) UNSIGNED NOT NULL, `uname` varchar(20) DEFAULT NULL, `ucreatetime` datetime DEFAULT NULL, `age` int(11) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT; -- -- 转存表中的数据 `usertb` --INSERT INTO `usertb` (`id`, `uname`, `ucreatetime`, `age`) VALUES (1, '用户1', '2010-01-01 00:00:00', 20), (2, '用户2', '2010-01-01 00:00:00', 20), (3, '用户3', '2010-01-01 00:00:00', 20), (4, '用户4', '2010-01-01 00:00:00', 20), (5, '用户5', '2011-01-01 00:00:00', 20), (6, '用户6', '2011-01-01 00:00:00', 20), (7, '用户7', '2011-01-01 00:00:00', 20), (8, '用户8', '2012-01-01 00:00:00', 20), (9, '用户9', '2012-01-01 00:00:00', 20), (10, '用户0', '2012-01-01 00:00:00', 20), (11, '用户1', '2010-01-01 00:00:00', 20), (12, '用户2', '2010-01-01 00:00:00', 20), (13, '用户3', '2010-01-01 00:00:00', 20), (14, '用户4', '2010-01-01 00:00:00', 20), (15, '用户5', '2011-01-01 00:00:00', 20), (16, '用户6', '2011-01-01 00:00:00', 20), (17, '用户7', '2011-01-01 00:00:00', 20), (18, '用户8', '2012-01-01 00:00:00', 20), (19, '用户9', '2012-01-01 00:00:00', 20), (20, '用户0', '2012-01-01 00:00:00', 20), (21, '用户1', '2010-01-01 00:00:00', 20), (22, '用户2', '2010-01-01 00:00:00', 20), (23, '用户3', '2010-01-01 00:00:00', 20), (24, '用户4', '2010-01-01 00:00:00', 20), (25, '用户5', '2011-01-01 00:00:00', 20), (26, '用户6', '2011-01-01 00:00:00', 20), (27, '用户7', '2011-01-01 00:00:00', 20), (28, '用户8', '2012-01-01 00:00:00', 20), (29, '用户9', '2012-01-01 00:00:00', 20), (30, '用户0', '2012-01-01 00:00:00', 20), (31, '用户1', '2010-01-01 00:00:00', 20), (32, '用户2', '2010-01-01 00:00:00', 20), (33, '用户3', '2010-01-01 00:00:00', 20), (34, '用户4', '2010-01-01 00:00:00', 20), (35, '用户5', '2011-01-01 00:00:00', 20), (36, '用户6', '2011-01-01 00:00:00', 20), (37, '用户7', '2011-01-01 00:00:00', 20), (38, '用户8', '2012-01-01 00:00:00', 20), (39, '用户9', '2012-01-01 00:00:00', 20), (40, '用户0', '2012-01-01 00:00:00', 20), (41, '用户1', '2010-01-01 00:00:00', 20), (42, '用户2', '2010-01-01 00:00:00', 20), (43, '用户3', '2010-01-01 00:00:00', 20), (44, '用户4', '2010-01-01 00:00:00', 20), (45, '用户5', '2011-01-01 00:00:00', 20), (46, '用户6', '2011-01-01 00:00:00', 20), (47, '用户7', '2011-01-01 00:00:00', 20), (48, '用户8', '2012-01-01 00:00:00', 20), (49, '用户9', '2012-01-01 00:00:00', 20), (50, '用户0', '2012-01-01 00:00:00', 20), (51, '用户1', '2010-01-01 00:00:00', 20), (52, '用户2', '2010-01-01 00:00:00', 20), (53, '用户3', '2010-01-01 00:00:00', 20), (54, '用户4', '2010-01-01 00:00:00', 20), (55, '用户5', '2011-01-01 00:00:00', 20), (56, '用户6', '2011-01-01 00:00:00', 20), (57, '用户7', '2011-01-01 00:00:00', 20), (58, '用户8', '2012-01-01 00:00:00', 20), (59, '用户9', '2012-01-01 00:00:00', 20), (60, '用户0', '2012-01-01 00:00:00', 20), (61, '用户1', '2010-01-01 00:00:00', 20), (62, '用户2', '2010-01-01 00:00:00', 20), (63, '用户3', '2010-01-01 00:00:00', 20), (64, '用户4', '2010-01-01 00:00:00', 20), (65, '用户5', '2011-01-01 00:00:00', 20), (66, '用户6', '2011-01-01 00:00:00', 20), (67, '用户7', '2011-01-01 00:00:00', 20), (68, '用户8', '2012-01-01 00:00:00', 20), (69, '用户9', '2012-01-01 00:00:00', 20), (70, '用户0', '2012-01-01 00:00:00', 20), (71, '用户1', '2010-01-01 00:00:00', 20), (72, '用户2', '2010-01-01 00:00:00', 20), (73, '用户3', '2010-01-01 00:00:00', 20), (74, '用户4', '2010-01-01 00:00:00', 20), (75, '用户5', '2011-01-01 00:00:00', 20), (76, '用户6', '2011-01-01 00:00:00', 20), (77, '用户7', '2011-01-01 00:00:00', 20), (78, '用户8', '2012-01-01 00:00:00', 20), (79, '用户9', '2012-01-01 00:00:00', 20), (80, '用户0', '2012-01-01 00:00:00', 20), (81, '用户1', '2010-01-01 00:00:00', 20), (82, '用户2', '2010-01-01 00:00:00', 20), (83, '用户3', '2010-01-01 00:00:00', 20), (84, '用户4', '2010-01-01 00:00:00', 20), (85, '用户5', '2011-01-01 00:00:00', 20), (86, '用户6', '2011-01-01 00:00:00', 20), (87, '用户7', '2011-01-01 00:00:00', 20), (88, '用户8', '2012-01-01 00:00:00', 20), (89, '用户9', '2012-01-01 00:00:00', 20), (90, '用户0', '2012-01-01 00:00:00', 20), (91, '用户1', '2010-01-01 00:00:00', 20), (92, '用户2', '2010-01-01 00:00:00', 20), (93, '用户3', '2010-01-01 00:00:00', 20), (94, '用户4', '2010-01-01 00:00:00', 20), (95, '用户5', '2011-01-01 00:00:00', 20), (96, '用户6', '2011-01-01 00:00:00', 20), (97, '用户7', '2011-01-01 00:00:00', 20), (98, '用户8', '2012-01-01 00:00:00', 20), (99, '用户9', '2012-01-01 00:00:00', 20), (100, '用户0', '2012-01-01 00:00:00', 20); -- -- 转储表的索引 ---- -- 表的索引 `usertb` -- ALTER TABLE `usertb` ADD UNIQUE KEY `id` (`id`);

3、matlab测试代码
functions = calc_area(w,h) s = "ID:" + w + " 姓名:" + h; end

4、注意事项 python 版本 2.7
matlab 代码需放到python项目中
5、程序运行结果 【python调用matlab脚本】python调用matlab脚本
文章图片

    推荐阅读