Hi all, Im new to python and I need some help.
I have tried to get this piece of code to work for quite some time now. I finally made it work by using two while-loops.
THE PROBLEM
What I really wanted to do at the first place was to just have one while loop and a function that would take care of reading one row and save it to the different files. Then just repeat it with the next row and another call to the read-row function. The bad part that I saw was that I can't send the string (s in my code) as an argument to the function.
So my first question is: Can you send a string as an argument to a function? Or is it a better way?
More info about my program:
The T2000901.txt is build like this:
28.618382 27.631520 44
28.618382 27.631520 22
28.618382 27.631520 22
28.618382 27.631520 22
28.618382 116.999987 210
................And so on..................
Where each rows contains the values of a input/output at different times.
I wanted it to look more like this:
SigAngle = [ 28.618382 28.618382 28.618382 ...and so on... ]
RefAngle = [ 27.631520 27.631520 27.631520...and so on... ]
PWMDudy = [ 44 22 22 ...and so on... ]
This is achived by creating 3 different files that write it in this "format"
inFile = open('T2000901.txt', 'r') # Read the file of your choise
SigAngle = open('sigangle.txt', 'w') # Write to a file
SigAngle.write('SigAngle = [ ')
RefAngle = open('refangle.txt', 'w') # Write to a file
RefAngle.write('RefAngle = [ ')
PWMDudy = open('PWMDudy.txt', 'w') # Write to a file
PWMDudy.write('PWMDudy = [ ')
s = inFile.readline()
string = ('\t')
string2 = ('\n')
while s != "": # Loop until end of file
i = 0
k = i
while s[i] != string: # Look for a tab
i+=1
# The last "number" in this while loop contains a '\t', so skip that.
SigAngle.writelines(s[k:i])
SigAngle.write(' ') # Make a space!
i+=1
k = i # Save next value as start at the next while loop.
while s[i] != string:
i+=1
# The last "number" in this while loop contains a '\t', so skip that.
RefAngle.writelines(s[k:i])
RefAngle.write(' ') # Make a space!
i+=1
k = i # Save next value as start at the next while loop.
while s[i] != string2:
i+=1
# The last "number" in this while loop contains a '\n', so skip that.
PWMDudy.writelines(s[k:i])
PWMDudy.write(' ') # Make a space!
s = inFile.readline()
By the way: Im trying to use the KISS rule (Keep It Simple Stupid) and I have a bad habbit of commenting stuff that is pretty easy to understand and don't comment stuff that is harder to understand. At least I try to comment ;)
I have two more question if that is ok.
If I know want to insert all information to one file instead of the three seperate files, how can I do that in a simple way?
My guess is of using: output = open('howdy.m', 'a')
And then maybe something like: output.writelines(sigangle.txt)
Is this right or am I completely wrong?
The final question: Now I will soon write 4 different files but will only be using the last for the processing in MATLAB. I guess there is a way of doing this without the need of the 3 files (sigangle.txt, refangle.txt & PWMDudy.txt). Is this difficult?
WHAT IS THIS CODE FOR? :
This only concern the people who have the time and/or want to know more about what this code should do. The T2000901.txt contains data with 3 rows. The rows are SignalAngle, ReferenceAngle and PWMDudy. Signalangle is the angle you get from the robot's joint, ReferenceAngle is the angle the joint should go to and the PWMDudy is something sent to the DC-motor of the joint to make it move.
This because we want to see how different settings in the robot will affect the robot's joint.
To be able to compare we will process the data in MATLAB. But to be able to do that we have to translate T2000901.txt to a, for example, howdy.m file.
What's the name of the robot? AIBO. It is really sweet until you start program it and it won't do what you thought you told it to do. ;)
WHY PYTHON? :
I have programmed some in Java and got tired of it, so I wanted to test something new. A friend of mine recommended Python so here I am trying to learn.
If anything is unclear please ask me and I will try to answer as soon as possible.
Thanks in advance / Sebastian