How can i translate a python code to matlab?
2 views (last 30 days)
Show older comments
Hello guys, how are you? so, i have a python code and i want to run it on matlab or translate to matlab, i don't know which one is possible. It#s a really simple code, just get a file with a big matrix and divide that on small matrixes in a class..
my code:
import linecache
print ("Put the name of the file here")
filename = input()
def getlineTable(n): #Function to get the numbers from the file
line = linecache.getline(filename, n)
vet = [float(it) for it in line.split()]
return vet
def getlinedata(n): #Function to get the Header from the file
line = linecache.getline(filename, n, )
return line
class Data: #Class data. Need to check the parameters on the file
time = []
Fx = []
Fy = []
Fz = []
x = []
y = []
z = []
temperature = []
data = Data
n=15 #PUT THE n HERE!!
j=0
i = 0
while True:
i+=1
p = getlinedata(i)
if p[0]=='T' and p[1]=='i' and p[2]=='m' and p[3]=='e':
i+=1
p = getlineTable(i)
while j<(2*n): #the pair lines of the matrixes are the forward motion and the odd lines are the backward motion
data.time.append([])
data.Fx.append([])
data.Fy.append([])
data.Fz.append([])
data.x.append([])
data.y.append([])
data.z.append([])
data.temperature.append([])
while True:
if j%2 == 0 and p[5]==985.215:
break
if j%2 == 1 and p[5]==515.415:
break
#appending the values from the file
data.time[j].append([p[0]])
data.Fx[j].append([p[1]])
data.Fy[j].append([p[2]])
data.Fz[j].append([p[3]])
data.x[j].append([p[4]])
data.y[j].append([p[5]])
data.z[j].append([p[6]])
data.temperature[j].append([p[7]])
i+=1
p = getlineTable(i)
p[5] = round(p[5],3)
j+=1
break
I wish you can help me.. Thank you.
0 Comments
Answers (1)
Steven Lord
on 19 Oct 2016
Which release are you using? Starting in release R2014b you can use Python functions and objects in MATLAB. See the Release Notes for more information as well as details about enhancements to that functionality that have been added since R2014b. You should be able to search the documentation installed with your release of MATLAB for "call python" and it should find pages with instructions for how to use the functionality included in your release.
2 Comments
Steven Lord
on 19 Oct 2016
As I said, that functionality was introduced in release R2014b. Your release is older than that, so you won't be able to use it. I don't know of any automated conversion tool, so you're probably going to have to manually convert the code (or upgrade to a release where you can use the functionality introduced in R2014b.)
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!