matlab如何调用python脚本文件,路径是怎样的

matlab如何调用python脚本文件,路径是怎样的,第1张

  Python是纯粹的自由软件, 源代码和解释器CPython遵循 GPL(GNU General Public License)协议。Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进。

  Python具有丰富和强大的库。它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C++/C++)很轻松地联结在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适的语言改写,比如3D游戏中的图形渲染模块,性能要求特别高,就可以用C/C++重写,而后封装为Python可以调用的扩展类库。需要注意的是在您使用扩展类库时可能需要考虑平台问题,某些可能不提供跨平台的实现。

  matlab调用Python的脚本文件

  matlab把所有参数输出到一个文件里,然后用system命令调python脚本。python脚本读文件做计算结果再写文件。最后matlab再读文件得到结果。假设python脚本的用法是:

  

  python xxx.py in.txt out.txt

  则matlab调用的命令:

  [status, cmdout] = system(‘python xxx.py in.txt out.txt’)

  Matlab的system函数用来向 *** 作系统发送一条指令,并得到控制台的输出,可以直接将控制台的输出在Command Window打印出来,或者保存在变量中。 与system类似的还有dos函数和unix函数,我觉得它们都是对system函数的一种包装,而Matlab的system函数也许是对C的库函数system的包装。

  先编写一个调用Python脚本的matlab程序即python.m

  funcTIon [result status] = python(varargin)

  % call python

  %命令字符

  cmdString=‘python’;

  for i = 1:nargin

  thisArg = varargin{i};

  if isempty(thisArg) | ~ischar(thisArg)

  error([‘All input arguments must be valid strings.’]);

  elseif exist(thisArg)==2

  %这是一个在Matlab路径中的可用的文件

  if isempty(dir(thisArg))

  %得到完整路径

  thisArg = which(thisArg);

  end

  elseif i==1

  % 第一个参数是Python文件 - 必须是一个可用的文件

  error([‘Unable to find Python file: ’, thisArg]);

  end

  % 如果thisArg中有空格,就用双引号把它括起来

  if any(thisArg == ‘ ’)

  thisArg = [‘“’, thisArg, ‘”’];

  end

  % 将thisArg加在cmdString后面

  cmdString = [cmdString, ‘ ’, thisArg]

  end

  %发送命令

  [status,result]=system(cmdString);

  end

  就可以用这个函数调用python脚本了。 下面就来个调用python脚本matlab_readlines.py(保存在matlab当前目录)的例子

  import sys

  def readLines(fname):

  try:

  f=open(fname,‘r’)

  li=f.read().splitlines()

  cell=‘{’+repr(li)[1:-1]+‘}’

  f.close()

  print cell

  except IOError:

  print “Can‘t open file ”+fname

  if ’__main__‘==__name__:

  if len(sys.argv)《2:

  print ’No file specified.‘

  sys.exit()

  else:

  readLines(sys.argv[1])

  这个脚本用来读取一个文本文件,并生成Matlab风格的cell数组的定义字符串,每个单元为文本的一行。 放了一个测试用的文本文件test.txt在Matlab的Current Directory中,内容如下:

  This is test.txt

  It can help you test python.m

  and matlab_readlines.py

  测试:

  在Matlab的Command Window中输入:

  》》 str=python(’matlab_readlines.py‘,’test.txt‘);

  》》 eval([’c=‘ str])

  c =

  ’This is test.txt‘ [1x29 char] [1x23 char]

  》》 celldisp(c)

  c{1} = This is test.txt

  c{2} = It can help you test python.m

  c{3} = and matlab_readlines.py

  matlab如何调用python脚本文件的路径

  Python作为一个用途广泛的语言,提供了不少用于 *** 作目录和文件路径的方法。而Matlab虽然一开始是为了数学运算而设计的,但是同样提供了不少 *** 作路径的函数,因为数学运算也少不了要和文件打交道。下面列出Matlab和Python中功能相同或相似的 *** 作路径的方法,相信对于需要同时使用Matlab和Python的技术人员而言,有一些的作用。下面的讨论都是基于Python2.6.4和Matlab7.7。

  (1) filesep - os.path.sep

  filesep是Matlab的一个函数,用于返回当前平台的目录分隔符,Windows是,Linux是/。在Python中对应物为os.path.sep,但它不是一个函数,而是一个字符串。

  (2) fullfile - os.path.join

  Matlab函数fullfile用于将若干个字符串连接成一个完整的路径,如Matlab帮助文档中的例子:

  f = fullfile(‘C:’, ‘ApplicaTIons’, ‘matlab’, ‘myfun.m’)

  f =

  C:ApplicaTIonsmatlabmyfun.m

  其在Python中的对应物为os.path.join函数,例如:

  》》》 os.path.join(‘c:\’, ‘lab’, ‘test.py’)

  ‘c:\lab\test.py’

  但是在Windows平台下os.path.join和Matlab函数fullfile的行为有些许不同,例如,我们用上面Matlab例子中的路径给os.path.join:

  》》》 os.path.join(‘C:’, ‘ApplicaTIons’, ‘matlab’, ‘myfun.m’)

  ‘C:Applications\matlab\myfun.m’

  比较fullfile和os.path.join的结果,发现os.path.join的结果中C:后面没有添加分隔符。这并不是os.path.join的bug,而是它考虑了Windows中C:与C:的区别:“C:”表示C盘,而“C:”表示当前目录,例如

  C:LAB》cd c:book

  C:LABook》

  和下面的例子是等效的:

  C:LAB》cd 。ook

  C:LABook》

  (3) fileparts - os.path.split, os.path.splitext

  Matlab函数filesep用于将一个完整的文件名分割为四个部分:路径,文件名,扩展名,版本号。在Python中可以用os.path.split和os.path.splitext取得路径,文件名,扩展名,至于版本号用哪个我不太清楚。

  (4) pathsep - os.path.sep

  Matlab函数pathsep返回当前平台的路径分隔符。Windows平台为‘;’,Linux为‘:’。在Python中的对应物为os.pathsep,但它不是一个函数而是一个字符串。

  (5) exist - os.path.exists

  实际上Matlab函数exist和Python的os.path.exists有很大的不同。之所以放在一起,是因为它们都可以用于判断目录或者文件是否存在。Matlab函数exist的功能要复杂很多,不像os.paht.exists只返回True和False,exist函数的返回值为整数,不同的数值代表了不同的含义。详情参阅Matlab help。

  (6) which - inspect.getsourcefile

  Matlab函数which可以通过一个函数或脚本名称得到它的完整路径。不仅如此,which还能处理函数重载的情况,例如:

  》》 which abs(0)

  built-in (D:ProgramMATLABR2008b oolboxmatlabelfun@doubleabs) % double method

  》》 which abs(single(0))

  built-in (D:ProgramMATLABR2008b oolboxmatlabelfun@singleabs) % single method

  在Python中与之功能类似的函数是inspect.getsourcefile,例如:

  》》》 import random

  》》》 import inspect

  》》》 inspect.getsourcefile(random)

  ‘D:\Program\Python26\lib\random.py’

  (7) isdir - os.path.isdir

  Matlab函数isdir和Python的os.path.isdir都用于判断一个路径是否代表了一个目录。

  (8) dir - os.listdir

  Matlab函数dir和os.listdir都用于列出一个目录的内容,但两者有区别。os.listdir的返回值为list类型,包含了目录内文件和目录的名称,而Matlab函数dir的返回值为结构体数组类型,包含了如下的域:

  name:文件或目录的名称;

  date:修改日期

  bytes:文件大小

  isdir:是否是目录

  datenum:修改日期

  详细用法参考Matlab help。

  (9) cd - os.chdir

  Matlab函数cd用于切换当前工作目录。Python中的对应物为os.chdir。IPython中可以直接用cd。

  (10) pwd - os.getcwd

  Matlab函数pwd返回当前工作目录的路径,Python中对应物为os.getcwd。在IPython中可直接用pwd。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/dianzi/2717703.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-17
下一篇 2022-08-17

发表评论

登录后才能评论

评论列表(0条)

保存