hi I'm Paul and I'm a software developer at MathWorks in this video I want to share with you
some of our recommended ways of loading data from MATLAB into seaman link using root level import
blocks and to help you avoid some common pitfalls along the way there are a number of different ways
to import data into a model that you could choose from to satisfy your particular use
cases including using a from file from workspace from spreadsheet and signal editor blocks but for
this video I'm gonna focus on loading using root level import blocks you might use an import block
in this way when you already have data in your workspace perhaps from a previous simulation
that you would like to use as inputs into another model now when I say root level what I mean is the
import block is not inside a subsystem or inside a referenced model you can only load data from
the MATLAB workspace into an import at the root level so if you need to load data directly into
a subsystem or reference model you might want to consider using one of these other blocks to
get started let's take a look at the model we're gonna load data into this very simple
model has two root level input ports well load data from the workspace into each and pass the
signal into a reference model that will calculate the moving average I'll focus only on the loading
of data into the ports and I won't really discuss the algorithm but I'll include all models in an
example code and links in the notes for this video in case you want to go through them for
yourself first we need to create some data in the workspace to load into these ports and I'll
create two variables in the workspace one for each port the first variable is a time series
object with data representing a straight line with noise added to it I want samples ranging
from 0 to 10 seconds with a spacing of tenth of a second note how I'm creating the time vector here
by first creating an array of integers from 0 to 100 and multiplying that vector by the increment
size and this matches how similan calculates time steps a common pitfall is to create a time vector
using linspace or 0 : 1 : 10 which may result in unexpected behavior due to floating-point
precision rounding especially when loading into an input port with a discrete sample time I'll
include links for more information on this common pitfall in the video notes the second variable is
a time table object that holds sine wave data that is offset with some noise now typically you'd try
to use the same format for all your variables but I kind of want to showcase the different formats
available in case you're not aware what's possible and to show you that it's okay to mix and match
the allowed formats are arrays structure with and without time time series and time table however I
recommend using time series or time tables because they're the most feature-rich formats now let's
configure the model to load these two variables from the workspace under the modeling tab click
the gear icon to open up the configuration parameters and select the input check box enter
the names of the variables separated by a comma notice that the order here is important order
in the list matches the port order number when you're done click OK to close the configuration
parameters click play to run the simulation we can observe the output of the simulation
by double-clicking the scope blocks as you can see we have successfully loaded data into both ports
the loaded data is represented by the yellow curves and the moving average is represented
by the blue curves look at how assume link has logged the data into the workspace a new object
called out is now present this new object is of type simulation output and holds all the data log
to the MATLAB workspace including logs out a data set object that contains log data for each of the
signal logging antenna in your model you can also use a data set object to load into a model which I
will show you how to do next we will use the same model and same workspace data we're just packing
the data a little differently start by creating a data set object and then add a workspace data to
this object using the add method don't forget to assign the output to our data set object as data
sets our value classes note that there must be one element of the data set for each input port in the
model otherwise an error would be thrown during simulation now we need to configure the model to
load the data set as before click the gear icon to open up the configuration parameters select
input to be on and in the textbox add the name of our data set click play again to simulate the
model as we are loading the same data as before the curves and the scope blocks should look the
same okay it looks good as your models grow in size the number of input ports may start to make
the canvas look cluttered to avoid this you might consider loading the data through a single port
that outputs a bus signal let's see an example in this model we will load the same data as before
but through a single port and as a bus and we'll use a bus selector to plug out each signal and
directed to its model reference block to calculate the moving average first we need to create a
bus object in the base workspace open up the bus editor by typing buss editor at the command prompt
click Add bus and then click Add insert bus element twice well stick with the default
values for the bus elements double data type non-complex etc note that the bus optic is now
present in the workspace when loading a bus the data must be organized in the form of a
structure whose leaf elements each contain data for a signal in our case we have two signals so
we will create a struct with two leaf elements one will be our time series and the other will
be our time table note that the names of the structures fields must match the names
of the corresponding bus elements a and a1 now create a data set and add the structure as the
one element as before so the data set variable as the input in the configuration parameters
also we must set the data type of the input port as the bus data type
let's make sure that our bus selector block is outputting the correct signals
simulate and observe that the results are equivalent to our two previous simulations
okay so that's it click on the links in the video description to download the code and
sample models and try it out on your own you can also find links to documentation on various
topics discussed in this video including an example of how to stream big data into root
level import blocks also in the video description you will find a link to the route in port mapper
a graphical tool that you can use to import data into root level ports thanks for listening