matlab代码sqrt-matcall:在Python中使用MATLAB函数和类

  • T9_924316
    了解作者
  • 130.9KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-21 07:25
    上传日期
Matlab代码sqrt 马特卡尔 “用于Python的MATLAB引擎API”处理起来有些麻烦。 通过此Python模块,可以轻松地运行MATLAB函数并在Python代码中使用MATLAB类。 您可以像在MATLAB中一样在Python中执行几乎所有操作。 内容 以下内容包括: MatCaller ,它处理MATLAB。 MatFunction ,它通过MATLAB函数(包括构造函数)在Python中动态定义一个函数。 MatClass ,它从MATLAB类动态定义Python中的类。 MatStruct ,它使MATLAB的struct类似于对象。 %%matlab魔术命令。 准备 首先使用Python启动MATLAB,并通过mat = MatCaller()创建实例。 确保正确下载了适用于Python的MATLAB引擎API 。 from matcall import MatCaller import numpy as np mat = MatCaller () 要将路径“ C:/ Users / ...”添加到MATLAB引擎,请使用addpath(path)函数。 您还可以
matcall-main.zip
  • matcall-main
  • matcall
  • matcall.py
    20.8KB
  • const.py
    1.7KB
  • __init__.py
    71B
  • struct.py
    1.9KB
  • __pycache__
  • matcall.cpython-37.pyc
    18.5KB
  • __init__.cpython-37.pyc
    212B
  • img1.png
    105.2KB
  • LICENSE
    17.7KB
  • setup.py
    570B
  • README.md
    4.7KB
  • .gitignore
    1.8KB
内容介绍
# matcall ![](img1.png) "MATLAB engine API for Python" is a little bit troublesome to handle. This Python module makes it easy to run MATLAB functions and use MATLAB classes in Python codes. You can do almost anything in Python as in MATLAB. ## Contents Following included: - `MatCaller`, which handles MATLAB. - `MatFunction`, which dynamically defines a function in Python from MATLAB function (including constructor). - `MatClass`, which dynamically defines a class in Python from MATLAB class. - `MatStruct`, which makes MATLAB's `struct` like object. - `%%matlab` magic command. ## Preparation First launch MATLAB in Python and make an instance by `mat = MatCaller()`. **Make sure you have downloaded MATLAB engine API for Python correctly**. ```python from matcall import MatCaller import numpy as np mat = MatCaller() ``` To add a path "C:/Users/..." to MATLAB engine, use addpath(path) function. You can also recursively add directory paths that .m files are contained. ```python mat.addpath(r"C:\Users\...") mat.addpath(r"C:\Users\...", child=True) ``` ## MATLAB to Python conversion table |MATLAB|Python| |:----:|:----:| |`logical`|`bool`| |matrix (1x1)|`int` or `float`| |matrix (1xN)|1-dim `ndarray`| |matrix (Mx1)|1-dim `ndarray`| |matrix (MxN)|`ndarray`| |`char`|`str`| |`cell`|`list`| |`struct`|`MatStruct`| |`function_handle`|`MatFunction`| |others|`MatClass`| ## MATLAB to Python conversion table |Python|MATLAB| |:----:|:----:| |`bool`|`logical`| |`int` or `float`|matrix (1x1)| |`str`|`char`| |`list` or `tuple`|`cell`| |`dict` or `MatStruct`|`struct`| |`ndarray`|matrix| |`MatFunction`|`function_handle`| |`MatClass`|corresponding object| # Basic Usage ## Use MATLAB Console You can run MATLAB using `console` and obtain one object as return value. ```python out = mat.console() ``` ``` --- Input --- (MATLAB) In >>> a = 1:5; (MATLAB) In >>> b = sqrt(a); (MATLAB) In >>> return b ``` ```python out ``` ``` [Out] array([1. , 1.41421356, 1.73205081, 2. , 2.23606798]) ``` IPython magic is also supported. ```python %%matlab data.time = 1:100; data.signal = sin(data.time/5.2); data.name = "wave"; data ``` ``` [Out] data = struct with fields: time: [1x100 double] signal: [1x100 double] name: "wave" ``` ## Use MATLAB Functions MATLAB functions can be translated to Python function by ```python mMax = mat.translate("max") mMax ``` ``` [Out] MatFunction<max> ``` ```python mMax(np.array([3,6,4])) ``` ``` [Out] [6, 2.0] ``` MATLAB lambda function is also supported. ```python sq = mat.translate("@(t)t^2") sq ``` ``` [Out] MatFunction<@(t)t^2> ``` ```python sq(10) ``` ``` [Out] 100 ``` ## Use MATLAB Classes, Properties and Methods Translation of MATLAB class constructor is also possible. Here constructor (not the class itself!) is returned and Python class will be dynamically defined with it. Same object is sent to MATLAB workspace only when it's needed. ```python mycls = mat.translation("MyClass") obj = mycls(x1, ..., xn) obj ``` ``` [Out] MatClass<MyClass> ``` Setter and getter are also (mostly) defined so that you can deal with the properties in a very simple way. ```python mplot = mat.translate("plot") pl = mplot(x, y) # A figure window is openned here. pl.Color = "red" # The line color is changed to red here. ``` A struct object in MATLAB is translated to dict object in Python by default. However, it is a little bit troublesome to access the contents of dict, compared to MATLAB struct (at least you need to type double quotation every time). Thus, here in matcall a `MatStruct` object is returned instead. Following example shows how to solve ODE using MATLAB `ode45` function. ```python xlim = np.array([0., 20.]) v0 = np.array([2.,0.]).T vdp1 = mat.translate("vdp1") ode45 = mat.translate("ode45") result = ode45(vdp1, xlim, v0) result ``` ``` [Out] MatStruct with 6 fields: solver: ode45 extdata: MatStruct object (3 fields) x: np.ndarray (60,) y: np.ndarray (2, 60) stats: MatStruct object (3 fields) idata: MatStruct object (2 fields) ``` ## Operators and Built-in Functions Special methods are converted correctly so that almost all the calculations overloaded in MATLAB are supported in Python. The table below shows the examples of overloads. |MATLAB|Python|What is supported| |:----:|:----:|:---------------:| |`plus`|`__add__`, `__radd__`| `x + y`| |`eq`|`__eq__`|`x == y`| |`char`|`__str__`|`str(x), print(x)`| Following code is an example of calculating derivative using symbolic variables. ```python sym = mat.translate("sym") diff = mat.translate("diff") x = sym("x") A = sym("A") f = A * np.sin(x)**2 print(diff(f)) ``` ``` [Out] 2*A*cos(x)*sin(x) ```
评论
    相关推荐
    • Matlab-Python-Pictures-master.zip
      利用matlab绘制节点,以及他们之间的关系,利用Excel表格 adjoin.xlsx,描述点之间的关系,1表示连接,0表示不连接,pos.xlsx表示要绘制的屏幕上的位置
    • MATLABPython接口
      介绍MATLABPython接口的多种方法,包括MATLABPython的相互调用以及将MATLAB代码打包成组件然后在Python中集成。
    • matlab导入mat代码-MATLAB-PYthon-Combine-Code:MATLAB-PYthon-Combine-C
      MATLAB的官方网站,有关于如何为Python安装MATLAB API的 2. 相关代码 2.1 Python当中的 import matlab.engine eng = matlab.engine.start_matlab() res = eng.GetRowColOfImage('test_img.jpg') print res 2.2 ...
    • Matlab Python UDP Socket:Matlab中使用Python Socket实现UDP通信的基本示例-matl
      这个基本示例提供了一个使用 python 套接字实现的 UDP 通信接口。 我体验过这种方法在时间关键应用程序中运行比 matlab/java UDP 套接字更... 据我所知,所有 python 依赖项都应该由 Matlab 附带的 python 版本解决。
    • matlab2python
      git clone https://gitee.com/zongfang/matlab2python.git python matlab2python.py file.m -o file.py
    • matlabfunction代码生成-python:Python
      matlab function 代码生成 install python 在window下下载python安装包并安装(会自动安装pip和setuptools,如没有可以然后安装:python get-pip.py),然后添加脚本(如C:\Python27\Scripts或C:\Python36\Scripts)到...
    • matlab特征点代码-matlab-python:MatlabPython
      matlab特征点代码 matlab-python #1.将matlab代码转成python #2.特征点匹配图
    • TripInference:用于行程推断的MATLABPython代码
      该存储库包含用于Trip Trip的MATLAB / Python代码。 TripParseTest.m:演示TripParse.m功能的MATLAB脚本 TripParse.m:解析行程的主要功能 getActivities.py:用于活动匹配和Google查询的Python库 mainActivities....
    • Codes:MatlabPython脚本
      MatlabPython的脚本
    • matlab-with-python:结合使用MATLABPython的示例
      结合使用MATLABPython的示例 该演示演示了如何从MATLAB调用Python,反之亦然,以及如何从PythonMATLAB Compiler SDK)调用打包的MATLAB代码。 主要示例调用了开放天气API: : 没有提供访问密钥(ha),但是您...