Load nonfinite (Inf or NaN) data into simulink simulation

25 views (last 30 days)
Before R2022b it was possible to directly load a timeseries with nonfinite data into a Simulink model. Either by loading it directly from the workspace via the Data Import/Export tab of the Configuration Parameters, or by using a similar approach using a 'From Workspace' block.
As of R2022b the support for the following types is dropped: Simulink.Timeseries, Simulink.TsArray, Simulink.SubsysDataLogs, ScopeDataLogs, and StateflowDataLogs.
This means that I have to search for a new way to load nonfinite data into my model. The Simulink.Timeseries documentation suggests to use a Simulink.SimulationData.Dataset. Unfortunately this doesn't seem to be a valid option, as it throws the following error: "The Data property of timeseries must be a built-in numeric, logical, fixed point or enumerated type. Data must be finite (not Inf or NaN)."
What other options are available to load data into a simulation, if the data must be able to contain Inf or NaN?
  4 Comments
Evan Wouters
Evan Wouters on 23 Jan 2023
My use case is a unit test that verifies that a Simulink model and the code generated from that Simulink model behave similar. So that's not very application specific.
The generated code still supports nonfinite data and so does Simulink, so I want to keep testing this. I made a workaround where the nonfinite data is created in Simulink itself and not imported from the workspace. Then it remains possible to feed this nonfinite data into a (referenced) model or a subsystem.
Simulant
Simulant on 23 Jan 2023
I am testing safety relevant requirements and software components. These tests include robustness tests where inputs get not finite numbers.

Sign in to comment.

Answers (2)

Sachin Lodhi
Sachin Lodhi on 30 Aug 2023
Hi Evan, I understand that you are trying to load timeseries data in Simulink. Since “Simulink.Timeseries" is dropped in R2022b, you can use "Simulink.SimulationData.Dataset" to load data containing Inf or NaN as shown in the following example code:
% Create sample time values
time = 0.1:0.1:1;
% Create sample data vectors with NaN and inf values
data1 = [1, 2, NaN, 4, 5, Inf, 7, 8, 9, 10];
% Create timeseries objects
ts1 = timeseries(data1, time);
% Create a Simulink.SimulationData.Dataset object
dataset = Simulink.SimulationData.Dataset;
% Add timeseries objects to the dataset
dataset = dataset.addElement(ts1, 'Timeseries1');
The following are the other workarounds to handle Inf or NaN values in your data:
  1. Replace Inf or NaN values: Before adding the data to the "Simulink.SimulationData.Dataset", you can replace any Inf or NaN values with appropriate values or placeholders. For example, you can replace them with a large number or a specific value that represents missing data.
  2. Use a different data type: If you need to preserve Inf or NaN values in your dataset, you can consider using a different data type that supports them, such as cell arrays or structures. You can create a cell array or structure to store your timeseries objects along with any additional metadata.
The above workarounds can help you handle Inf or NaN values effectively within your dataset.
You can also use the "From Workspace" Block. I recommend you refer to the following documentation page to know more about loading data using the “From Workspace” block:
I hope this helps you import timeseries data.
  1 Comment
Kyle
Kyle on 14 Nov 2023
Edited: Kyle on 14 Nov 2023
This does not solve this issue, which is that when the model is actually run with the dataset:
simIn = Simulink.SimulationInput(mdl);
simIn = setExternalInput(simIn,dataset);
sim(simIn)
an error will be thrown. It does not appear that a model can be run with a dataset that contains Inf or NaN values. Is there any other way to programmatically set the external inputs for a model that contains multiple input ports?

Sign in to comment.


Guilherme Costa Nascimento
The only way that I know of to bring in NaN and Inf data into Simulink is with the Playback block, available starting in R2022b: https://www.mathworks.com/help/simulink/slref/playback.html
Other loading blocks, such as the Signal Editor, From Workspace, and Inport blocks, do not support loading Inf or NaN values. I believe this has been the case since R2016b.
  8 Comments
Guilherme Costa Nascimento
Enrico, there's no one answer to this and depends how you are using the MATLAB code. If you want to run a script only once during the simulation, model callbacks are the best way to do it. If instead you want to run your MATLAB code at every timestep, then you can use either MATLAB Function blocks or MATLAB System blocks. See this page for details on which one to pick: Integrate MATLAB Code into Simulink.
If you use MATLAB Function blocks but don't want to copy all of your code into MATLAB Function blocks, you can just call your MATLAB files from the MATLAB Function block. See this example for details.

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!