How to access a matlab function within a forloop from python

1 回表示 (過去 30 日間)
Arshad Pasha
Arshad Pasha 2018 年 10 月 8 日
回答済み: Harry Vancao 2018 年 10 月 12 日
Hello All,
I am trying to access a matlab function from python. I have carried out all the initial setup steps and I am also able to acess a simple script and functions from python. However, I want to access a matlab function within a forloop from python. This is the previous reference link which I used.
file1= Test1.m
classdef Test1 < handle
properties (Access = private)
input1 =0;
input2 = 0;
end
methods
function out1 = ADD_1(this,i1,i2)
% Test1.input1 = i1;
% Test1.input2 = i2;
%
% out1 = Test1.input1 + Test1.input2;
%
this.input1 = i1;
this.input2 = i2;
% out1 = this.input1 + this.input2;
out1 = i1 + i2;
plot(out1);
end
function out2 = SUB_1(this,out1)
out2 = out1 - 1;
end
end
end
file2 = call_Test1.m
obj1 = Test1();
out_store = [];
for i = 1:5
o1 = obj1.ADD_1(12,i);
o2 = obj1.SUB_1(o1);
out_store = [out_store;o1,o2];
end
disp(out_store);
plot(out_store);
in python,
import matlab.engine
eng = matlab.engine.start_matlab()
eng.addpath(r'C:\Users\Mat2Py_code_test', nargout = 0)
myobj = eng.Test1()
How to proceed further to call this Test1.m file from python which further accesses call_Test1.m

回答 (1 件)

Harry Vancao
Harry Vancao 2018 年 10 月 12 日
If you are interested in calling the script from call_Test1.m, you can simply convert that into a function itself and call it from python. Some documentation detailing this process can be found here: https://www.mathworks.com/help/matlab/matlab_external/call-user-script-and-function-from-python.html
If you are interested in calling the functions from within a for loop, you can instantiate an instance of the MATLAB object Test1() and then proceed to call the MATLAB function from within a python for-loop.
myobj = eng.Test1()
for i in range(5):
func1_out = end.ADD_1(myobj, 10, 20)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by