How to import table data (*.csv) in Scilab

Using tables is a convenient way to deal with data. Engineers, scientists, accountants and many other people are using tables to keep track and analyse data. There are several tools which allow table data handling, the most common being Microsoft Excel (*.xlsx) and Open Office/LibreOffice Calc (*.ods).

Scilab video tutorial

A common way of keeping table data accessible between different platforms can be done by using comma-separated values (*.csv) file format. A *.csv file is a text file in which a comma , is used to separate data values. A *.csv file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.

A data file in *.csv format can be easily imported in *.xlsx or *.ods format, or read into the table editors (Excel or Open Office), edited and saved back into *.csv format.

In this example we are going to use a *.csv file to define our data, which is going to be imported in Scilab and processed. The data represents the powertrain parameters and road load from two vehicles, a Fiat and a Renault. For this particular example we are going to use LibreOffice Calc to edit and save the *.csv file. The data is summarise in the table below.

Vehicle name S Fiat Panda 100 HP Renault Twingo GT
Engine speed [rpm] Ne N 1000 1700 2500 2700 3275
3850 4100 4250 4400 5200
6050 6300 6700 7000
1000 1700 1950 2500 3000
4250 4800 5400 6000
Engine torque [Nm] Te N 60 83 105 110 120 128 130
131 130 125 115 110 100 92
90 133 140 144 145 142 140
130 115
Gear ratios [-] ix N 3.55 2.16 1.48 1.12 0.92 0.77 3.73 2.05 1.39 1.03 0.80
Final drive ratio [-] i0 N 4.07 4.21
Aerodynamic drag [-] Cx N 0.34 0.32
Maximum transversal area [m2] A N 2.02 2.34
Vehicle mass [kg] m N 975 980
Tire size tire S 195/45R15 185/55R15
Road load coefficient 0 [-] f0 N 0.0133 0.0133
Road load coefficient 1 [-] f1 N 0.0000287 0.0000287
Road load coefficient 2 [-] f2 N 0.00000018 0.00000018
Road load coefficient 3 [-] f3 N 0 0
Air density [kg/m3] rho N 900 900
Gravitational acceleration [m/s2] g N 9.81 9.81
Road slope [%] slope N 0 10 35 0 10 35

The first column contains the description and unit of the data. The second column contains the variable name of the corresponding data, which is going to be used also in Scilab as variable name field. The third column describes what kind of data the line contains: N for numeric and S for string. This information is going to be used when importing the data in Scilab. The fourth and fifth columns are containing the actual data. Several columns, for other vehicles can be added, if needed.

All the data in the table above is going to be stored in a file called inputData.csv. To import the content of the file we are going to use the following Scilab instructions:

clear()
clc()
data=csvRead("inputData.csv",[],[],"string");
for i=1:max(size(data))
    if data(i,3)~="S"
        execstr(strcat(["v1.",data(i,2),"=[",data(i,4),"];"]))
        execstr(strcat(["v2.",data(i,2),"=[",data(i,5),"];"]))
    else
        execstr(strcat(["v1.",data(i,2),"=[""",data(i,4),"""];"]))
        execstr(strcat(["v2.",data(i,2),"=[""",data(i,5),"""];"]))
    end
end
clear i data CSVfile

The first two functions clear() and clc() are used to delete all the existing variables from Variable Browser and clean the Scilab Console. This is useful to be able to spot any warning/error messages and also see the impact of the instructions in the Variable Browser.

The function csvRead() reads all the content of the inputData.csv file and stores it as string in the variable data.

The for loop is used to parse the data and create two data structure variables: v1 for the first vehicle (Fiat) and v2 for the second vehicle (Renault). As you can see, we use the data identifier (S) in a conditional if loop to distinguish between string data and numerical data.

At the end, with the function clear(), we delete the intermediate variables from the Variable Browser and keep only the relevant ones.

All the instructions above can be entered in a Scilab script (*.sce) and run, to get the data imported.

After the data has been imported, by typing the variable v1 in the Scilab Console, followed by Enter key, we can display the content of the structure:

--> v1
v1 =
name: [1x1 string]
Ne: [1x14 constant]
Te: [1x14 constant]
ix: [1x6 constant]
i0: [1x1 constant]
Cx: [1x1 constant]
A: [1x1 constant]
m: [1x1 constant]
tire: [1x1 string]
f0: [1x1 constant]
f1: [1x1 constant]
f2: [1x1 constant]
f3: [1x1 constant]
rho: [1x1 constant]
g: [1x1 constant]
slope: [1x3 constant]
Pe: [1x14 constant]

As you can see, the data fields of the v1 data structure are the same with the identifiers used in the *.csv file. This way we can track and make sure that we have the right data imported. Further, we can check the content of the v1 structure by typing a dot . followed by the filed name:

--> v1.ix
ans =
3.55 2.16 1.48 1.12 0.92 0.77

The same applies for the v2 data structure variable.

Now that we have all the data in Scilab, we can go ahead and process it. First example is going to be a functions which reads the size marking of a tire and converts it into a numerical value (wheel radius). For a complete explanation about this, read the article How to calculate wheel radius. In the second example we are going to calculate the engine power, based on the engine speed and torque information, and plot it side by side for both vehicles.

The following Scilab instructions, for the wheel radius calculation, are saved into a Scilab function (symbol2radius.sci).

function y=symbol2radius(symbol)
    W = evstr(part(symbol,[1:3])); // tire width [mm]
    AR = evstr(part(symbol,[5:6])); // tire aspect ratio
    drim = evstr(part(symbol,[8:9]))*2.54*10; // rim diameter [mm]
    H = AR * W/100; // tire height [mm]
    rs = (drim + 2*H)/(2*1000); // wheel static radius [m]
    y=rs;
endfunction

First, we need to load the function into the Scilab environment by running the file. Second, we can call the function as:

--> symbol2radius(v1.tire)
ans =
0.27825

--> symbol2radius(v2.tire)
ans =
0.29225

As you can see, the argument of the function is the tire marking symbol, imported from the *.csv file, which is stored in the v1.tire and v2.tire structures as string data. To store the static radius for both vehicles, we can create another data field .rs in the v1 and v2 structures as:

--> v1.rs = symbol2radius(v1.tire);
--> v2.rs = symbol2radius(v2.tire);

Next, we are going to calculate the engine power Pe [BHP] function of engine speed and torque:

\[P_{e} \text{ [BHP]} = N_{e} \cdot \frac {\pi}{30} \cdot T_{e} \cdot 1000 \cdot 1.341\]

where:

Ne [rpm] – engine speed
Te [Nm] – engine torque

The Scilab instructions for engine power calculations are:

//Calculate engine power [bhp]]
v1.Pe = (v1.Ne*%pi/30).*v1.Te*1e-3*1.341;
v2.Pe = (v2.Ne*%pi/30).*v2.Te*1e-3*1.341;

The values of the engine power, for both vehicles, are stored in the .Pe fields of the v1 and v2 data structures.

Next, we are going to plot the engine torque and power, function of the engine speed. The Scilab instructions are listed below.

// Plot engine torque
figure(0)
clf()
plot(v1.Ne, v1.Te,'r-')
plot(v2.Ne, v2.Te,'b-')
xgrid()
xlabel('Engine speed [rpm]')
ylabel('Engine torque [Nm]')
title('x-engineer.org')
ha=gca()
hf=gcf()
hf.background = -2;
ha.data_bounds = [0,0;8000,150];
legend(v1.name,v2.name,'in_lower_right')

// Plot engine power
figure(1)
clf()
plot(v1.Ne, v1.Pe,'r-')
plot(v2.Ne, v2.Pe,'b-')
xgrid()
xlabel('Engine speed [rpm]')
ylabel('Engine power [BHP]')
title('x-engineer.org')
ha=gca()
hf=gcf()
hf.background = -2;
ha.data_bounds = [0,0;8000,120];
legend(v1.name,v2.name,'in_lower_right')

By running the Scilab instructions we get the following graphical images:

Engine torque plot

Image: Engine torque plot

Engine power plot

Image: Engine power plot

The benefit of this approach is that the data in the *.csv file can be easily changed and imported into Scilab. This example can also be extended to include more vehicles or process any other type of data.

One Response

  1. Babula Samal

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh