Load nonfinite (Inf or NaN) data into simulink simulation

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 Kommentare

I would like to "push" this question.
Before 2022b I loaded NaN and Inf values to the From Workspace Block using a TimeSeries. In 2022b the TimeSeries is not supported anymore for the From Workspace, but I still need to load not finite numbers to the From Workspace block.
If I use another type including an NaN value, for example an array, I get the error message: "Unsupported input format for From Workspace block 'model/From Workspace'. Available formats are double non-complex matrix, a structure with or without time, or a structure with MATLAB timeseries as leaf nodes. All formats require the data to be finite (not Inf or NaN)."
Is there indeed no way to load not finite numbers to the From Workspace Block in Matlab 2022b?
If so, will MathWorks remove this bug / feature in 2023a again?
I checked the 2022b release notes and didn't see anything about those types being dropped. I saw it on the doc page, surprised it wasn't in the release notes.
Anyway, if I may ask, what are the the use cases for loading NaN or Inf data into the model?
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.
I am testing safety relevant requirements and software components. These tests include robustness tests where inputs get not finite numbers.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Sachin Lodhi
Sachin Lodhi am 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 Kommentar

Kyle
Kyle am 14 Nov. 2023
Bearbeitet: Kyle am 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?

Melden Sie sich an, um zu kommentieren.

Guilherme Costa Nascimento
Guilherme Costa Nascimento am 15 Nov. 2023

0 Stimmen

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 Kommentare

Enrico
Enrico am 20 Sep. 2024
Bearbeitet: Enrico am 20 Sep. 2024
Hi, any update for the current versions of 2024?
I tried with multiple options including timeseries or Simulink.SimulationData.Dataset or Simulink.SimulationData.Signal but no solutions works.
Hi Enrico,
The Playback block continues to be the only way to bring in NaN and Inf data.
Have you had issues using the Playback block?
Hi Guilherme,
as of now I've not tried to use this block as it was relatively new to me.
I'd like to extract data from an array coming from a .mat file recalled inside the callback InitFcn.
Will it be possible to do it even for enumerative set of data?
Thanks in advance
Hi Enrico,
Yes, the Playback block supports enumerations and you can pull straight from a .mat file, without the need for any callbacks. I find that timeseries are the easiest way to work with the Playback block.
Using the Playback block doesn't preclude you from using other methods of bringing in data in the same model. So, for instance, you could still use an Inport to bring in the enumerated data, and then use the Playback block for the data that contains Inf or NaN values.
Hope this helps.
@Guilherme Costa Nascimento Thanks a lot Guilherme, much appreciated.
@Guilherme Costa Nascimento Guilherme, to your knowledge, is it possible to run scripts only in Simulink? If I have scripts that are called during a Matlab run can I maintain the same code architecture within Simulink without converting all of them into Matlab block functions? I was wondering if there's a less time consuming solution.
I confirm to you that Playback block works well, thanks again for your advice.
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.
@Guilherme Costa Nascimento Thanks again for the valuable help!

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2022b

Gefragt:

am 13 Dez. 2022

Kommentiert:

am 30 Okt. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by