Denis
Denis

Reputation: 29

Function doesn't return right values

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?

Upvotes: 1

Views: 80

Answers (2)

martineau
martineau

Reputation: 123393

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

Upvotes: 0

Jay Atkinson
Jay Atkinson

Reputation: 3287

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)

Upvotes: 1

Related Questions