Guy on Simulink

Simulink & Model-Based Design

Importing Data From Excel

Today I want to share a useful tip for importing signals from Excel into a simulation.

From Spreadsheet block

In case you were not aware, since R2015b, Simulink has offered the From Spreadsheet block.

From Spreadsheet block

Using this block, you can specify an Excel file, sheet name, and range of the data.

From Spreadsheet dialog

Configuring the From Spreadsheet block

Let's take a simple example Excel sheet like the following, where we have points sampled at every 0.1 seconds:

Excel sheet

If I configure the From Spreadsheet block as in the above image, specifying that the first column is time, I will very likely see either duplicate or skipped points in the simulation.

Excel sheet results

Why is that? This is due to the same phenomenon as I explained in this previous post about importing discrete signal data in Simulink

If we compare the "equally spaced" time points from the Excel spreadsheet, you will notice that they are slightly different from the equally spaced time points taken by Simulink:

Compare Excel vs Simulink

In this example, when Simulink takes a point at 13.6sec, it sees that the data contains no point at exactly 13.6sec and holds the previous value from 13.5sec. At 13.7sec, it will output the last point it saw in the data, the one at 13.6000000000099sec.

How can you avoid this situation?

To avoid this confusion between the steps taken by Simulink and the steps specified in the Excel sheet, what I recommend is to configure the From Spreadsheet block to ignore the time column in the Excel sheet.

To do that, in the block dialog, set the Sample Time to the appropriate value, then set the Treat first column as to Data and specify the Range to start at the second column:

Compare Excel vs Simulink

That way, the From Spreadsheet block will simply use one row of the Excel sheet at every time step.

Now it's your turn

Are you using the From Spreadsheet block? Did you run in this issue?

Let us know if you have suggestions on how we could improve the From Spreadsheet block to avoid this situation.

|
  • print

コメント

コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。