我是靠谱客的博主 任性硬币,最近开发中收集的这篇文章主要介绍matlab调用python库_Matlab调用Python,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

ABAQUS可以利用Python脚本进行后处理,但是前处理使用Python多有不便,借助便于应用的Matlab语言可以使ABAQUS前处理工作达到事半功倍的效果。有时候后处理需要用到模型的一些数据,比如提取某些单元的应力,这就需要知道单元编号,如果单元过多的话就需要利用Matlab写成Python文件,而后运行Python提取数据,往往Python提取数据需要储存的文本文件中,不能与Matlab进行时时交互,即不能成为Matlab的一个变量。为了使ABAQUS的前后处理工作成为一个有机的整体,就需要Matlab能够调用Python。有以下步骤:

1.新建一个python.m文件,见下文。

2.假设matlab安装路径为D:Program Files,在D:Program

FilesMATLABR2011atoolboxmatlab中新建

一个文件夹python

3.将python.m文件放在文件夹python中

4.在matlab软件界面中加载python.m文件:file->setpath->addfolder->matlab文件夹下的toolbox文件夹- >文件夹python

5.在MATLAB中的用法为:r=python('*.py','*','*'),例如,r=python('plus.py','3','5'),其中plus.py为

用户的程序,求解两个数相加,由于matlab与python的数据格式有差别,函数的参数可以以字符串输入,之

后转换为python数据格式,返回值同样再转换为字符串格式。供matlab识别。

6.目前,仍有一问题没有解决,单独安装的python和matlab衔接没有问题,abaqus自带的python却无法连接,

不知是否是盗版软件的问题,总是提示缺少一些动态链接库文件。

-----------------------------------------------------------------------------------------

其中plus.py内容为:

import sys

def plus(x,y):

z=

x+y

return

z

if __name__ == '__main__':

x

= float(sys.argv[1]) #第一个参数

y

= float(sys.argv[2]) #第二个参数

sys.stdout.write(str(plus(x,y)))

-----------------------------------------------------------------------------------------

其中python.m内容为:

function [result status] =

python(varargin) cmdString = '';

% Add input to arguments to operating

system command to be executed.

% (If an argument refers to a file on the MATLAB path, use full

file path.)

for i = 1:nargin

thisArg =

varargin{i};

if

~ischar(thisArg)

error(message('MATLAB:python:InputsMustBeStrings'));

end

if

i==1

if exist(thisArg, 'file')==2

% This is a valid file on the MATLAB path

if isempty(dir(thisArg))

% Not complete file specification

% - file is not in current directory

% - OR filename specified without extension

% ==> get full file path

thisArg = which(thisArg);

end

else

% First input argument is pythonFile - it must be a valid

file

error(message('MATLAB:python:FileNotFound', thisArg));

end

end

%

Wrap thisArg in double quotes if it contains spaces

if

isempty(thisArg) || any(thisArg == ' ')

thisArg = ['"', thisArg, '"']; %#ok

end

%

Add argument to command string

cmdString =

[cmdString, ' ', thisArg]; %#ok

end

% Execute python script

errTxtNopython = 'Unable to find python executable.';

if isempty(cmdString)

error(message('MATLAB:python:NopythonCommand'));

elseif ispc

%

PC,注意修改Python的安装路径

pythonCmd = 'D:Program FilesPython26';

cmdString =

['python' cmdString];

pythonCmd =

['set PATH=',pythonCmd, ';%PATH%&' cmdString];

[status,

result] = dos(pythonCmd);

else

% UNIX

[status

ignore] = unix('which python'); %#ok

if (status

== 0)

cmdString = ['python', cmdString];

[status, result] = unix(cmdString);

else

error('MATLAB:python:NoExecutable', errTxtNopython);

end

end

% Check for errors in shell command

if nargout < 2 && status~=0

error(message('MATLAB:python:ExecutionError', result,

cmdString));

end

参考文献

最后

以上就是任性硬币为你收集整理的matlab调用python库_Matlab调用Python的全部内容,希望文章能够帮你解决matlab调用python库_Matlab调用Python所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部