Difference between revisions of "Writing a CSV File"
Line 3: | Line 3: | ||
Several of the V-REP simulation scenes require a comma-separated values (CSV) file as input. A CSV file may represent the trajectory of a robot, where each row contains the configuration of the robot at an instant in time, with a fixed time between each row. If the robot has <math>n</math> joints, then each row has <math>n</math> numbers separated by commas. For example, five rows of a CSV file for a six-joint robot might look like this: |
Several of the V-REP simulation scenes require a comma-separated values (CSV) file as input. A CSV file may represent the trajectory of a robot, where each row contains the configuration of the robot at an instant in time, with a fixed time between each row. If the robot has <math>n</math> joints, then each row has <math>n</math> numbers separated by commas. For example, five rows of a CSV file for a six-joint robot might look like this: |
||
2.950000,-1.570000,0.000000,0.000000,0.000000,0.000000 |
2.950000, -1.570000, 0.000000, 0.000000, 0.000000, 0.000000 |
||
2.987484,-1.540050,0.019967,0.019992,0.012495,0.009996 |
2.987484, -1.540050, 0.019967, 0.019992, 0.012495, 0.009996 |
||
3.024875,-1.510399,0.039734,0.039933,0.024958,0.019967 |
3.024875, -1.510399, 0.039734, 0.039933, 0.024958, 0.019967 |
||
3.062079,-1.481344,0.059104,0.059775,0.037360,0.029888 |
3.062079, -1.481344, 0.059104, 0.059775, 0.037360, 0.029888 |
||
3.099002,-1.453174,0.077884,0.079468,0.049667,0.039734 |
3.099002, -1.453174, 0.077884, 0.079468, 0.049667, 0.039734 |
||
Below are code snippets in Python, MATLAB, and Mathematica that you can modify to create your own CSV files. |
Below are code snippets in Python, MATLAB, and Mathematica that you can modify to create your own CSV files. |
Revision as of 22:25, 3 July 2018
Several of the V-REP simulation scenes require a comma-separated values (CSV) file as input. A CSV file may represent the trajectory of a robot, where each row contains the configuration of the robot at an instant in time, with a fixed time between each row. If the robot has joints, then each row has numbers separated by commas. For example, five rows of a CSV file for a six-joint robot might look like this:
2.950000, -1.570000, 0.000000, 0.000000, 0.000000, 0.000000 2.987484, -1.540050, 0.019967, 0.019992, 0.012495, 0.009996 3.024875, -1.510399, 0.039734, 0.039933, 0.024958, 0.019967 3.062079, -1.481344, 0.059104, 0.059775, 0.037360, 0.029888 3.099002, -1.453174, 0.077884, 0.079468, 0.049667, 0.039734
Below are code snippets in Python, MATLAB, and Mathematica that you can modify to create your own CSV files.
This page contains information on getting started quickly with V-REP. This page contains a number of scenes that accept CSV file inputs for visualization of robot trajectories.
Python
import numpy as np # Generate random 3x4 matrix of floats y, 3x1 vector of ints d y = np.random.rand(3, 4) d = np.random.randint(-100, 100, 3) # Open a file for output # Overwrite f = open("output.csv", "w") # Append #f = open("output.csv", "a") # For loop running 3 times to print each csv row for i in range(len(d)): output = " %10.6f, %10.6f, %10.6f, %10.6f, %d\n" % (y[i,0], y[i,1], y[i,2], y[i,3], d[i]) f.write(output) # close file f.close()
The code below is a somewhat simpler version.
import numpy as np # Generate random 3x4 matrix of floats y, 3x1 vector of ints d y = np.random.rand(3, 4) d = np.random.randint(-100, 100, 3) # Set number precision y = np.round(y, 6) # Overwrite csv file np.savetxt("output.csv", np.asarray(np.c_[y, d]), delimiter = ",")
MATLAB
% Generate random 3x4 matrix of floats y, 3x1 vector of ints d y = rand(3, 4); d = randi([-100, 100], 3, 1); % Open a file for output % Overwrite f = fopen('output.csv', 'w'); % Append %f = fopen('output.csv', 'a'); % For loop running 3 times to print each csv row for i = 1: length(d) fprintf(f, ' %10.6f, %10.6f, %10.6f, %10.6f, %d\n', y(i, :), d(i)); end % Close file fclose(f);
The code below is a somewhat simpler version.
% Generate random 3x4 matrix of floats y, 3x1 vector of ints d y = rand(3, 4); d = randi([-100, 100], 3, 1); % Set number precision y = round(y, 6); % Overwrite csv file csvwrite('output.csv', [y, d]);
Mathematica
(* Generate random 3x4 matrix of floats y,3x1 vector of ints d *) y = RandomReal[1, {3, 4}]; d = RandomInteger[{-100, 100}, {3, 1}]; (* Open a file for output *) (* Overwrite *) f = OpenWrite[FileNameJoin[{NotebookDirectory[], "output.csv"}]]; (* Append *) (* \ f=OpenAppend[FileNameJoin[{NotebookDirectory[],"output.csv"}]]; *) (* For loop running 3 times to print each csv row *) Do[WriteString[f, ExportString[{Flatten[{SetAccuracy[y[[i, ;;]], 6], d[[i]]}]}, "CSV"]], {i, Length[d]}]; (* Close file *) Close[f];
The code below is a somewhat simpler version.
(* Generate random 3x4 matrix of floats y,3x1 vector of ints d *) y = RandomReal[1, {3, 4}]; d = RandomInteger[{-100, 100}, {3, 1}]; (* Set number precision *) y = SetAccuracy[y, 6]; (* Overwrite csv file *) Export[FileNameJoin[{NotebookDirectory[], "output.csv"}], ArrayFlatten[{{y, d}}], "CSV"];