How can I call a python-Function in Matlab with an Array as input and return Value?

12 views (last 30 days)
Hello Community
I am re-Organizing Arrays in Matlab. Since I am a python-Person, I wrote a program that simply rearranges the order of the Array-Content i created in matlab. I need the re-organized Array again in Matlab so I would like to do the following:
Dummy code all within matlab:
1 Matlab-createdArray [randomsize]
2 NewOrderArray = py.Rearrange(Matlab-CreatedArray)
3... do whatever with my newOrderArray
I will attach my python idea aswell, maybe one of you can help me to convert that into Matlab and dont call python in the end, that be the smoother way I guess.
For those who care, the idea of my re-arrange is to basically re-order the array in a Middle-First way (like middle-out sort but backwards). So an order of 1,2,3,4,5 becomes 1,5,3,2,4 and so on.
I am happy for every input, thanks a lot in advance!
def fundamentalfunc( num1 , num2 ):
mid = int( ( num1+num2 ) / 2 )
return [num1, mid , mid+1, num2], mid
def layerGenerator( whatImReallyInterestedIn, previousLayer ):
newLayer= [ ]
for i in range(0, len( previousLayer ), 2):
dummy,element = fundamentalfunc(previousLayer[ i ],previousLayer[ i+1 ] )
whatImReallyInterestedIn.append( element )
newLayer.extend( dummy )
return newLayer
def uniquifyer( whatImReallyInterestedIn, num2 ):
consecutiveList = list( range( 1, num2+1 ) )
simplifiedList = [ ]
for i in range( 0, len( whatImReallyInterestedIn ) ):
focus = whatImReallyInterestedIn[i]
for j in range( 0,len( consecutiveList ) ):
if focus == consecutiveList[j]:
simplifiedList.append( consecutiveList[ j ] )
consecutiveList.pop( j )
break
return simplifiedList
num2= 20
whatImReallyInterestedIn = [ ]
newLayer = [1, num2]
while len(whatImReallyInterestedIn) < num2:
newLayer = layerGenerator( whatImReallyInterestedIn, newLayer )
whatImReallyInterestedIn = uniquifyer( whatImReallyInterestedIn, num2 )
print(whatImReallyInterestedIn)

Answers (1)

Deepak Meena
Deepak Meena on 9 Sep 2020
Hey Wilhelm,
My understanding is that you want to import the functionality of a python function. It's very common practise and very easy to use to as well.
From the MATLAB command prompt, add the current folder to the Python search path.
if count(py.sys.path,'') == 0
insert(py.sys.path,int32(0),'');
end
Consider the following example to use python functionality in MATLAB:
N = py.list({'Jones','Johnson','James'})
N = Python list with no properties. ['Jones', 'Johnson', 'James']
Similarly in this way you can create your python array/list in MATLAB.
Now let's suppose your file name is myfile.py and code structure is like this:
myfile.py
def Rearrange(OldArray):
% your alogorithm
return NewArray
Now you can call this functionas in MATLAB:
names = py.myfile.Rearrange(OldArray);
Here Names will also be the Python list/array.
Hope this clear your doubt. For more detail refer to this document 

Community Treasure Hunt

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

Start Hunting!