How to access a matlab function within a forloop from python

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Call 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