Skip to main content

Read csv in your model

A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. 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 CSV file typically stores tabular data (numbers and text) in plain text, in which case each line will have the same number of fields.

Save an Excel spreadsheet as a CSV file

  • In your Excel spreadsheet, click File.
  • Click Save As.
  • Click Browse to choose where you want to save your file.
  • Select "CSV" from the "Save as type" drop-down menu.
  • Click Save.
CSV file

C++ include

Make sure that you include in global project header:

#include <iostream>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <vector>
#include <cmath>
#include <sstream>

In Model Explorer, select your Project Package and go on the C/C++ property tab:

C++ include

Model activity

Create matching type

First, you need to create a type that will store the data from your csv. For each row in your csv, you will create a type that will hold the type of every column. Then you need to create a list containing this type to store every row. For example, if your csv file looks like this:

intial data file csv

You could create types to match your csv file, as follow:

matching types csv

Create the variable

You now need to create in your lifeline the variable in which you will store the data :

variable creation for csv

Internal activity implementation

Create a scenario, and an internal activity for the csv to be read. In order for your activity to be implemented for csv purpose, you can just right click on the activity > Edit > Complete Activity.

complete activity for csv

Then, select the option Load Csv File:

select load csv file

Click next. A variable configuration table appears. This is where you set which variable in your lifeline you want to use to store the data from your csv. Check what is related to your csv as in the following example:

csv variable configuration

Click next. You need to indicate the file name of your csv file (you need to place your csv file next to the generated executable of your model):

csv file name configuration

You shouldn't change the separator. However, you can click the generate button in the example section:

csv generate example button

The generated file will be a very simple example of what is expected in your csv for your activity to work properly.
This csv file will be placed in the "export" folder.

csv file example
caution
  1. The executable of the model will only be able to read a csv by running it with the Launcher.
  2. Please also place your executable and csv in your C disk (for read/write rights).
  3. By default, the csv file must be placed at the same level as the generated executable, due to the path to read it. You will need to change this path manually if you do not want to place the csv and executable in the same place.