Load timeseries data from workspace for non-periodic data

I want to load timeseries data from workspace to use as my model inputs. The data is a timeseries, but with points only at the times when the value changes (to make the files more managable - if I save all the samples, the files are huge even though the data is mostly invariant).
For examples:
ts.Time = [0 50]
ts.Data = [1 100]
What I want, is the input to be continuously 1 for times between 0 and 50, and then step change to 100 and continue at this value until the end of the simulation. This has to work irrespective of the simulation time step.
When I load the data and run the model, there only seems to be two choices:
1) If I tick the 'Interpolate data' box then for times when there isn't an exact match, the value of the input is interpolated between available samples. If you take my original example, between times 0 and 50 the input will slowly ramp at each simulation timestep instead of a step change at 50.
2) If I clear the 'Interpolate data' box then the value is only set when the simulation time matches the time value in the timeseries. For other times, the input is set to 0. Again, in my example the input shows two short blips at 0 and 50, but then inserts 0 for other timesteps.
Many thanks

 Accepted Answer

In the Model Configuration parameters,
change the Input field under Load From Workspace from ts to
addsample(ts,'Data',ts.Data(end),'Time',realmax)
assuming that ts is a timeseries object.

1 Comment

Thanks, that's a really neat solution.
For my case, it's still not 100% as I'm trying to map a MAT file containing the timeseries as an input to the Test Manager file. The timeseries object is buried inside a Simulink.SimulationData object, which adds a level of complication. But predominantly, I don't want the users to have to manually edit the 'Input string' after clicking 'Map Inputs'.
I'm just going to use the trick you've demonstrated to post process the data.
Many thanks!

Sign in to comment.

More Answers (1)

I seem to be able to get the desired behavior using the following code to assign a structure, unchecking the interpolate box, and selecting the hold option for forming output after final value
ts.time = [0;50]
ts.signals.values = [1;100]
also works with ts as a time series
ts = timeseries([1 100],[0 50])

5 Comments

If you are still having problems, please attach a Simulink model and an .m file to initialize ts, that demonstrates the problem.
Thanks Jon, but my question is about loading of the data for root level inputs, not using the 'From Workspace' block (sorry - I wasn't quite clear about this).
Your comment made me realize that part of what I said was not true: the inputs are held at the correct value, but not after the last sample of the timeseries has been reached. In that case, the value is pulsed for one execution and then set to 0.
What I'm after is exactly what you've shown in the screen shot ('Form output after final value') but applied to the method of loading the data that I'm using.
I can post-process my data to add a final value with a large timestep, but I'm hoping that there's a tickbox for it somewhere.
Thanks
OK, I poked around a little, and was not able to find a solution to hold the value when the simulation time exceeds the last time value in the timeseries assigned to the root level inport.
I found an old question where someone faced exactly the same issue as you, but there was no satisfactory solution. https://www.mathworks.com/matlabcentral/answers/41731-inport-in-simulink-doesn-t-hold-last-value
Is there some reason why you can't use the from workspace block, as it would at least in this regard, provide the behavior you want?
I don't want to use 'From Workspace', as I'm adding functionality to an existing workflow which uses Test Manager and mapping of MAT files as an input for tests. If I was to use From Workspace, I'd need to restructure the test harness models which then has impact on other things.
I understand. Glad you got an approach that will help you from @Paul

Sign in to comment.

Categories

Products

Release

R2021b

Asked:

on 10 Jan 2024

Commented:

Jon
on 11 Jan 2024

Community Treasure Hunt

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

Start Hunting!