1

I am stuck on this problem for hours now.

I have written a Python code to read and convert data from a text file and everything is running fine.

simulink_robot_motor1=[]
with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
 rows=0
 for line in data_file:
 rows=rows+1
 columns=len(line.split(","))
 simulink_robot_motor=[[0 for x in range(columns)] for y in range(rows)]
 i=0
 with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
 for line in data_file:
 current_line = line.split(",")
 current_line = list(map(float, current_line))
 simulink_robot_motor[i]=current_line
 i=i+1

I am interested in the simulink_robot_motor variable which has the following results:

[[0.0, 3.6],
[1.6e-06, 3.6],
[4.57e-06, 3.6],
[7.67e-06, 3.6],
[1.09e-05, 3.6],
...

Now, I would like to use this code within a function. So if I call the function the list simulink_robot_motor should be returned.

def get_matlab_sensor_data(): 
 simulink_robot_motor1=[]
 with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
 rows=0
 for line in data_file:
 rows=rows+1
 columns=len(line.split(","))
 simulink_robot_motor=[[0 for x in range(columns)] for y in range(rows)]
 i=0
 with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
 for line in data_file:
 current_line = line.split(",")
 current_line = list(map(float, current_line))
 simulink_robot_motor[i]=current_line
 i=i+1
 return (simulink_robot_motor)

But if I run get_matlab_sensor_data() I get the following result:

 [[0, 3.6],
 [0, 0],
 [0, 0],
 [0, 0],
 [0, 0],
 ...

I tried smaller data sets and also switched off scientific decimal style. However, it still didn't work. Is my loop not running properly?

asked Apr 18, 2017 at 16:38
1
  • might help to provide example input & expected output. Commented Apr 18, 2017 at 17:01

2 Answers 2

1

Your return statement is at the incorrect indent level, so it's getting executed too soon inside the for loop and never finishes the loop.

def get_matlab_sensor_data(): 
 simulink_robot_motor1=[]
 with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
 rows=0
 for line in data_file:
 rows=rows+1
 columns=len(line.split(","))
 simulink_robot_motor=[[0 for x in range(columns)] for y in range(rows)]
 i=0
 with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
 for line in data_file:
 current_line = line.split(",")
 current_line = list(map(float, current_line))
 simulink_robot_motor[i]=current_line
 i=i+1
 return (simulink_robot_motor)

Also, you probably don't need to indent the second with statement:

def get_matlab_sensor_data(): 
 simulink_robot_motor1=[]
 with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
 rows=0
 for line in data_file:
 rows=rows+1
 columns=len(line.split(","))
 simulink_robot_motor=[[0 for x in range(columns)] for y in range(rows)]
 i=0
 with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
 for line in data_file:
 current_line = line.split(",")
 current_line = list(map(float, current_line))
 simulink_robot_motor[i]=current_line
 i=i+1
 return (simulink_robot_motor)
answered Apr 18, 2017 at 17:03
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, in your original code, you're reading the entire file twice, which isn't necessary. Here's a simplification:

simulink_robot_motor=[]
with open(filename,'r') as data_file:
 for line in data_file:
 current_line = list(map(float, line.split(',')))
 simulink_robot_motor.append(current_line)
print(simulink_robot_motor) # -> [[0.0, 3.6], [1.6e-06, 3.6], [4.57e-06, 3.6], [7.67e-06, 3.6], [1.09e-05, 3.6]]

When you tried to convert your code into a function, there were two problems, one was simulink_robot_motor became a local variable that doesn't exist outside the function. The second was you had a return statement in the second for line in data_file: loop which means it would return after reading only one line.

The following fixes both issues and shows how to use the new function:

def get_matlab_sensor_data():
 results=[]
 with open(filename,'r') as data_file:
 for line in data_file:
 current_line = list(map(float, line.split(',')))
 results.append(current_line)
 return results
simulink_robot_motor = get_matlab_sensor_data()
print(simulink_robot_motor) # -> same results as before
answered Apr 18, 2017 at 17:08

2 Comments

Thanks @martineau. You answer helped a lot.
I can't due to my low reputation. I will when I will reach a score of 15.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.