| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → xPC Target |
| Contents | Index |
| Learn more about xPC Target |
| On this page… |
|---|
Creating a Simple Simulink Model Entering Parameters for the Scope Block Adding a Simulink Outport Block Entering Parameters for the Outport Blocks |
Before you can create a target application, you need to create a Simulink model. The software xPC Target then uses the Simulink model, the Real-Time Workshop environment, and a third-party compiler to create the target application. This tutorial uses a simple Simulink model to explain the tasks you need to do with the software xPC Target. If you are an experienced Simulink user, you can skip creating this model.
The model includes a transfer function and a signal generator block. If you want to visualize signals while simulating your model, you need to add a standard Simulink Scope block.
In the MATLAB Command Window, type
simulink
The Simulink library window opens.
From the File menu, point to New, and then click Model.
A blank Simulink model window opens.
In the left pane, double-click Simulink, and then click Continuous.
In the right pane, the Simulink library shows a list of blocks.

Click and drag the Transfer Fcn block to the Simulink model window.
Adjust the Numerator and the Denominator values to reflect the example.
In the Simulink Library Browser window, click and drag the following blocks to your model.
Click Sources, and add a Signal Generator block.
Click Sinks, and add a Scope block.
Click Signal Routing, and add a Mux block.
Note If you provide a name for a signal in the Signal name property of the Signal Properties dialog box, that name appears in the target PC GUI scope graph after you build and download the model to the target PC. By default, if you do not enter a name for the signal in this dialog box, the scope graph displays the signal identifier rather than a name. |
Double-click the Signal Generator block. The Block Parameters dialog box opens. From the Wave form list, select square.
In the Amplitude text box, enter
1
In the Frequency text box, enter
20
From the Units list, select rad/sec.
Your Block Parameters dialog box will look similar to the following figure.

Double-click the Transfer Fcn block.
Edit the Numerator and Denominator parameters to match those in the following figure.
Connect the Signal Generator block to the Transfer Fcn block, and connect the input and output signals to the scope block using the Mux block.
Your model should look similar to the figure shown.

From the File menu, click Save As and enter a filename. For example, enter xpc_osc and then click OK.
You can use either a Simulink Scope block or an xPC Target Scope block to visualize signals from an xPC Target application running in real time. The topics for this example describe how to use the xPC Target Scope block. See Adding an xPC Target Scope Block. See Signal Tracing with Simulink External Mode for a description of how to use a Simulink Scope block to visualize target application signals.
For information on creating a Simulink model and adding signal and scope blocks, see the online Simulink documentation.
You enter or change scope parameters to specify the x-axis and y-axis in a Scope window. Other properties include the number of graphs in one Scope window and the sample time for models with discrete blocks.
After you add a Scope block to your Simulink model, you can enter the scope parameters for signal tracing:
In the Simulink window, double-click the Scope block.
A Scope window opens.
![]()
A Scope parameters dialog box opens.
Click the General tab. In the Number of axes box, enter the number of graphs you want in one Scope window. For example, enter 1 for a single graph. Do not select the floating scope check box.
In the Time range box, enter the upper value for the time range. For example, enter 1 second. From the Tick labels list, choose all.
From the Sampling list, choose Sample time and enter 0 in the text box. Entering 0 indicates that Simulink evaluates this block as a continuous-time block. If you have discrete blocks in your model, enter the Fixed step size you entered in the Configuration Parameters dialog box.
Your Scope parameters dialog box will look similar to the figure shown below.

Click Apply to apply the changes to your model and leave the dialog box open.
Click OK to apply the changes to your model and close the Scope parameters dialog box.
In the Scope window, point to the y-axis shown in the figure below, and right-click.

The Scope properties: axis 1 dialog box opens. In the Y-min and Y-max text boxes, enter the range for the y-axis in the Scope window. For example, enter -2 and 2 as shown in the figure below.

Click Apply to apply the changes to your model and leave the dialog box open.
Click OK to apply the changes to your model and close the Axes Parameters dialog box.
If you want to log signal data to the MATLAB workspace for analysis and later save that data to a disk, you need to add a Simulink Outport block and activate logging from the Configuration Parameters dialog box.
The following procedure uses the Simulink model xpc_osc.mdl as an example. To create this model, see Creating a Simple Simulink Model.
xpc_osc
The Simulink block diagram opens for the model xpc_osc.mdl.

In the Simulink window, from the View menu, click Library Browser.
The Simulink Library Browser window opens.

In the left pane, double-click Simulink, and then click Sinks.
In the right pane, the browser shows a list of sink blocks.

Click and drag the Out1 block to your model and connect it to the output of the Mux block.
Your model should look similar to the figure shown.

From the File menu, click Save As and enter a filename. For example, enter xpc_osc1 and then click OK.
Your next task is to enter parameters for the Outport block. See Entering Parameters for the Outport Blocks.
During a simulation, Simulink saves signal data to MATLAB variables using Outport blocks. The default MATLAB variables are tout, xout, and yout. While running a real-time application, The xPC Target interface uses these same variables to pass signal data to target object parameters. A target object is a structure in the MATLAB workspace that the xPC Target software uses to interact with a target application. The default target object is tg, and the default parameters are Time, tg.States, and tg.Output.
After you add an Outport block to your Simulink model, you can enter parameters. This procedure uses the model xpc_osc1.mdl with an Outport block as an example. To add an Outport block, see Adding a Simulink Outport Block.
xpc_osc1
A Simulink window with the model xpc_osc1 opens.

In the Simulink window, from the Simulation menu, click Configuration Parameters.
The Configuration Parameters dialog box is displayed for the model.
Simulink displays the Solver pane. The Simulation section of this pane defines the initial stop and sample time for your target application.
In the Solver options section, enter 0 seconds in the Start time box. In the Stop time box, enter an initial stop time. For example, enter 20 seconds. To change this time after creating your target application, change the target object property tg.Stoptime to the new time using the MATLAB command-line interface. To specify an infinite stop time, enter inf.
From the Type list, select Fixed-step. Real-Time Workshop does not support variable-step solvers.
The Solver pane dialog changes.
From the Solver list, select a solver. For example, select the general-purpose solver ode4 (Runge-Kutta).
In the Fixed step size box, enter the sample time for the target application. For example, enter 0.00025 second (250 microseconds). You can change this value after creating the target application.
If you find that 0.000250 second results in overloading the CPU on the target PC, try a larger Fixed step size such as 0.002 seconds.
If your model contains discrete states, which would lead to a hybrid model with both continuous and discrete states, the sample times of the discrete states can only be multiples of the Fixed step size. If your model does not contain any continuous states, enter 'auto', and the sample time is taken from the model.
The Solver pane should look similar to the figure shown below.

Click the Data Import/Export node.
The Data Import/Export pane opens. This pane defines the model signals logged during a simulation of your model or while running your target application.
In the Save to workspace section of this pane, select the Time, States, and Output check boxes.
Note When your target application is running in real time, data is not saved to the variables tout and yout. Instead, data is saved to the target object properties TimeLog, StateLog, and OutLog. However, you must still select the Time, States, and Output check boxes for data to be logged to the target object properties. |
The Data Import/Export pane should look similar to the figure shown.

Normally you select all the Save to workspace check boxes. However, you might want to consider clearing some or all of them in the following cases:
Many states — If your model contains many states (for example, more than 20 states), the storage of the state vector requires a lot of target memory. If you clear the States check box, logging of states is turned off and more memory is available for the target application. An alternative to logging all the state signals is to select individual states of interest by adding Outport blocks to your model.
Small sample time — If you choose a very short sample time, this setting might overload the CPU. If you clear the Save to workspace check boxes, logging is turned off and more computing time is available for calculating the model.
Your next task is to add an xPC Target Scope block to your Simulink model. See Adding an xPC Target Scope Block.
The xPC Target software does not support the standard Simulink Scope blocks, but it does support xPC Target Scope blocks, which have unique capabilities when you use them with an xPC Target application. Do not confuse these xPC Target Scope blocks with standard Simulink Scope blocks.
Adding xPC Target Scope blocks to your Simulink model can save you time. When you build the model, the resulting target application contains the xPC Target Scope blocks you added to the model. After you download the target application, the xPC Target Scope block automatically displays on the target PC monitor. If you do not add xPC Target Scope blocks to your model and you want to monitor signals on the target application without rebuilding it, you need to add xPC Target scopes and define and select signals for the scopes (see Signals and Parameters in the xPC Target User's Guide). The signal information is saved with your model.
Note If you want to monitor an output signal from a Constant block by connecting it to an xPC Target Scope block, you must add a test point for the Constant block output signal. |
After you create a Simulink model, you can add an xPC Target Scope block. The following procedure uses the Simulink model xpc_osc1.mdl as an example to show how to connect an xPC Target Scope block to your model.
xpc_osc1
The Simulink block diagram opens for the model xpc_osc1.mdl.
In the Simulink window, from the View menu, click Library Browser.
The Simulink Library Browser window opens.

In the left pane, browse to and double-click xPC Target.
A list of I/O functions opens.

A list of miscellaneous group blocks opens.

Click and drag Scope (xPC) to your Simulink block diagram.
Simulink adds a new Scope block to your model with a scope identifier of 1.
Connect the xPC Target Scope block to the Simulink Scope block.
The model xpc_osc1.mdl should look like the figure shown.

From the File menu, click Save As. Enter a filename. For example, enter xpc_osc2 and then click OK.
Your next task is to define the xPC Target Scope block parameters. See Entering Parameters for an xPC Target Scope Block.
xPC Target Scope block parameters define the signals to trace on the scope and trigger modes. When the target application is downloaded to the target PC, the xPC Target kernel automatically creates the scope on the target. No additional definitions are necessary if you want a default scope on the target. This section describes how you can configure the xPC Target Scope block for other scope behavior.
After you add an xPC Target Scope block to your Simulink model, you can enter parameters for this block. To add an xPC Target Scope block, see Adding an xPC Target Scope Block. To enter the parameters for an xPC Target Scope block to write signal data to a file on the target PC, see Entering Parameters for an xPC Target Scope of Type File.
There are three types of scopes, target, host, and file. The xPC Target Scope block dialog changes depending on which scope type you are configuring. The following sections describe the procedure depending on the scope type:
This procedure uses the model xpc_osc2.mdl as an example.
xpc_osc2
The Simulink block diagram opens for the model xpc_osc2.mdl.

Double-click the block labeled Scope (xPC).
The Block Parameters: Scope (xPC) dialog box opens.

By default, the scope of type Target dialog is displayed.
In the Scope number box, a unique number to identify the scope is displayed. This number is incremented each time you add a new xPC Target Scope block. Normally, you do not want to edit this value.
This number identifies the xPC Target Scope block and the scope screen on the host or target computers.
From the Scope type list, select Target if it is not already selected.
The updated dialog box is displayed.
Select the Start scope when application starts check box to start a scope when the target application is downloaded and started. The scope window opens automatically.
From the Scope mode list, select Numerical, Graphical redraw, Graphical sliding, or Graphical rolling.
If you have a scope type of Target and a scope mode of Numerical, the scope block dialog adds a Numerical format box to the dialog. You can define the display format for the data. See step 7 for a description of how to complete the Numerical format box. If you choose not to complete the Numerical format box, the xPC Target software displays the signal using the default format of %15.6f, which is a floating-point format, with no label.
In the Numerical format box, enter a label and associated numeric format type in which to display signals. By default, the entry format is floating-point, %15.6f. The Numerical format box takes entries of the format
'[LabelN] [%width.precision][type] [LabelX]'
where
LabelN is the label for the signal. You can use a different label for each signal or the same label for each signal. This argument is optional.
width is the minimum number of characters to offset from the left of the screen or label. This argument is optional.
precision is the maximum number of decimal places for the signal value. This argument is optional.
type is the data type for the signal format. You can use one or more of the following types:
Type | Description |
|---|---|
%e or %E | Exponential format using e or E |
%f | Floating point |
%g | Signed value printed in f or e format depending on which is smaller |
%G | Signed value printed in f or E format depending on which is smaller |
LabelX is a second label for the signal. You can use a different label for each signal or the same label for each signal. This argument is optional.
Enclose the contents of the Numerical format field in single quotation marks.
For example,
'Foo %15.2f end'
For a whole integer signal value, enter 0 for the precision value. For example,
'Foo1 %15.0f end'
For a line with multiple entries, delimit each entry with a command and enclose the entire string in single quotation marks. For example,
'Foo2 %15.6f end,Foo3 %15.6f end2'
You can have multiple Numerical format entries, separated by a comma. If you enter one entry, that entry applies to each signal (scalar expansion). If you enter fewer label entries than signals, the first entry applies to the first signal, the second entry applies to the second signal, and so forth, and the last entry is scalar expanded for the remaining signals. If you have two entries and one signal, the software ignores the second label entry and applies the first entry. You can enter as many format entries as you have signals for the scope. The format string has a maximum length of 100 characters, including spaces, for each signal.
Select the Grid check box to display grid lines on the scope. Note that this parameter is only applicable for scopes of type Target and scope modes of type Graphical redraw, Graphical sliding, or Graphical rolling.
In the Y-Axis limits box, enter a row vector with two elements where the first element is the lower limit of the y-axis and the second element is the upper limit. If you enter 0 for both elements, then scaling is set to auto. Note that this parameter is only applicable for scopes of type Target and scope modes of type Graphical redraw, Graphical sliding, or Graphical rolling.
In the Number of samples box, enter the number of values to be acquired in a data package.
If you select a Scope mode of Graphical redraw, this parameter specifies the number of values to be acquired before the graph is redrawn.
If you select a Trigger mode other than FreeRun, this parameter can specify the number of samples to be acquired before the next trigger event.
If you select a Scope mode of Numerical, the block updates the output every Number of samples.
In the Number of pre/post samples box, enter the number of samples to save or skip. Specify a value less than 0 to save this number of samples before a trigger event. Specify a value greater than 0 to skip this number of samples after the trigger event before data acquisition begins.
In the Decimation box, enter a value to indicate that data should be collected at each sample time (1) or at less than every sample time (2 or greater).
From the Trigger mode list, select FreeRun.
If you select FreeRun or Software Triggering, the trigger event is an automatic one. No external trigger specification is required.
If you select Signal Triggering, then, in the Trigger signal box, enter the index of a signal. In the Trigger level box, enter a value for the signal to cross before triggering. From the Trigger slope list, select either, rising, or falling. You do not need to specify scope triggering.
If you select Scope Triggering, then in the Trigger scope number box, enter the scope number of a Scope block. If you use this trigger mode, you must also add a second Scope block to your Simulink model. You do not need to specify signal triggering.
If you select Scope Triggering and want the scope to trigger on a specific sample of the other scope, enter a value in the Sample to trigger on box. The default value is 0 and indicates that the triggering scope and the triggered (current) scope start simultaneously. For more information on this field, see Triggering One Scope with Another Scope to Acquire Data in the xPC Target User's Guide.
From the File menu, click Save As. The model is saved as xpc_osc2.mdl.
Your next task is to simulate the model. See Simulating the Model.
Note As soon as the target application is built and downloaded, the xPC Target kernel creates a scope. If you want to change xPC Target Scope parameters after building the target application or while it is running, you need to assign the scope to a MATLAB variable. To assign the scope object, use the target object method getscope. If you use the target object method getscope to remove a scope created during the build and download process, and then you restart the target application, the xPC Target kernel recreates the scope. |
This procedure uses the model xpc_osc2.mdl as an example.
xpc_osc2
The Simulink block diagram opens for the model xpc_osc2.mdl.

Double-click the block labeled Scope (xPC).
The Block Parameters: Scope (xPC) dialog box opens.
By default, the scope of type target dialog is displayed.
In the Scope number box, a unique number to identify the scope is displayed. This number is incremented each time you add a new xPC Target scope. Normally, you do not want to edit this value.
This number identifies the xPC Target Scope block and the scope screen on the host or target computers.
From the Scope type list, select Host.
The updated dialog box is displayed.

Select the Start scope when application starts check box to start a scope when the target application is downloaded and started. With a scope of type target, the scope window opens automatically. With a scope of type host, you can open a host scope viewer window from xPC Target Explorer.
In the Number of samples box, enter the number of values to be acquired in a data package.
In the Number of pre/post samples box, enter the number of samples to save or skip. Specify a value less than 0 to save this number of samples before a trigger event. Specify a value greater than 0 to skip this number of samples after the trigger event before data acquisition begins.
In the Decimation box, enter a value to indicate that data should be collected at each sample time (1) or at less than every sample time (2 or greater).
From the Trigger mode list, select FreeRun.
If you select FreeRun or Software Triggering, the trigger event is an automatic one. No external trigger specification is required.
If you select Signal Triggering, then in the Trigger signal box, enter the index of a signal. In the Trigger level box, enter a value for the signal to cross before triggering. From the Trigger slope list, select either, rising, or falling. You do not need to specify scope triggering.
If you select Scope Triggering, then in the Trigger scope number box, enter the scope number of a Scope block. If you use this trigger mode, you must also add a second Scope block to your Simulink model. You do not need to specify signal triggering.
If you select Scope Triggering and want the scope to trigger on a specific sample of the other scope, enter a value in the Sample to trigger on box. The default value is 0 and indicates that the triggering scope and the triggered (current) scope start simultaneously. For more information on this field, see Triggering One Scope with Another Scope to Acquire Data in the xPC Target User's Guide.
From the File menu, click Save As. The model is saved as xpc_osc2.mdl.
Your next task is to simulate the model. See Simulating the Model.
Note As soon as the target application is built and downloaded, the xPC Target kernel creates a scope. If you want to change xPC Target Scope parameters after building the target application or while it is running, you need to assign the scope to a MATLAB variable. To assign the scope object, use the target object method getscope. If you use the target object method remscope to remove a scope created during the build and download process, and then you restart the target application, the xPC Target kernel recreates the scope. |
In addition to logging signal data via a scope of type host, you can also have the xPC Target software save signal data to a file on the target PC C:\ hard drive or 3.5-inch disk drive.
After you add an xPC Target Scope block to your Simulink model, you can configure this block to save a file on the target PC.
Note The signal data file can quickly increase in size. You should examine the file size between runs to gauge the growth rate for the file. If the signal data file grows beyond the available space on the disk, the signal data might be corrupted. |
Saving signal data to files is most useful when you are using target PCs as stand-alone xPC Target systems. To access the contents of the signal data file that a xPC Target scope of type file creates, use the xPC Target file system object (xpctarget.fs) from a host PC MATLAB window. To view or examine the signal data, you can use the readxpcfile utility in conjunction with the plot function. For further details on the xpctarget.fs file system object and the readxpcfile utility, see Working with Target PC Files and File Systems in the xPC Target User's Guide. Saving signal data to files lets you recover signal data from a previous run in the event of system failure (such as a system crash).
To add an xPC Target Scope block, see Adding an xPC Target Scope Block.
This procedure uses the model xpc_osc2.mdl as an example.
xpc_osc2
The Simulink block diagram opens for the model xpc_osc2.mdl.

Double-click the block labeled Scope (xPC).
The Block Parameters: Scope (xPC) dialog box opens.
By default, the scope of type Target dialog is displayed.
In the Scope number box, a unique number to identify the scope that is displayed. This number is incremented each time you add a new xPC Target scope. Normally, you do not want to edit this value.
This number identifies the xPC Target Scope block and the scope screen on the host or target computer.
From the Scope type list, select File.
The updated dialog box is displayed.

Select the Start scope when application starts check box to start a scope when the target application is downloaded and started. The scope window opens automatically.
In the Number of samples box, enter the number of values to be acquired in a data package. This parameter works in conjunction with the AutoRestart check box. If the AutoRestart box is selected, the scope of type file collects data up to Number of samples, then starts over again, overwriting the buffer. If the AutoRestart box is not selected, the scope of type file collects data only up to Number of samples, then stops.
In the Number of pre/post samples box, enter the number of samples to save or skip. Specify a value less than 0 to save this number of samples before a trigger event. Specify a value greater than 0 to skip this number of samples after the trigger event before data acquisition begins.
In the Decimation box, enter a value to indicate that data should be collected at each sample time (1) or at less than every sample time (2 or greater).
From the Trigger mode list, select FreeRun, Software Triggering, Signal Triggering, or Scope Triggering.
If you select FreeRun or Software Triggering, the trigger event is an automatic one. No external trigger specification is required.
If you select Signal Triggering, then in the Trigger signal box, enter the index of a signal. In the Trigger level box, enter a value for the signal to cross before triggering. From the Trigger slope list, select either, rising, or falling. You do not need to specify scope triggering.
If you select Scope Triggering, then in the Trigger scope number box, enter the scope number of a Scope block. If you use this trigger mode, you must also add a second Scope block to your Simulink model. You do not need to specify signal triggering.
If you want the scope to trigger on a specific sample of the other scope, enter a value in the Sample to trigger on box. The default value is 0 and indicates that the triggering scope and the triggered (current) scope start simultaneously. For more information on this field, see Triggering One Scope with Another Scope to Acquire Data in the xPC Target User's Guide.
In the Filename box, enter a name for the file to contain the signal data. By default, the target PC writes the signal data to a file named C:\data.dat.
From the Mode list, select either Lazy or Commit. Both modes open a file, write signal data to the file, then close that file at the end of the session. With the Commit mode, each file write operation simultaneously updates the FAT entry for the file. This mode is slower, but the file system always knows the actual file size. With the Lazy mode, the FAT entry is updated only when the file is closed and not during each file write operation. This mode is faster, but if the system crashes before the file is closed, the file system might not know the actual file size (the file contents, however, will be intact). If you experience a system crash, you can expect to lose a WriteSize amount of data.
In the WriteSize box, enter the block size, in bytes, of the data chunks. This parameter specifies that a memory buffer of length Number of samples write data to the file in WriteSize chunks. By default, this parameter is 512 bytes, which is the typical disk sector size. Using a block size that is the same as the disk sector size provides optimal performance.
If you experience a system crash, you can expect to lose a WriteSize amount of data.
In the Number of samples box, enter the number of values to be acquired in a data package.
Select the AutoRestart check box to enable the scope of type file to collect data up to Number of samples, then start over again, appending the new data to the end of the signal data file. Clear the AutoRestart check box to have the scope of type file collect data up to Number of samples, then stop.
If the named signal data file already exists, the xPC Target software overwrites the old data with the new signal data.
Your next task is to simulate the model. See Simulating the Model.
The following sections describe how to simulate your model and run the target application. With scopes of type file, the xPC Target software generates a signal data file on the target PC after you run the target application. For further details on working with these files, see Working with Target PC Files and File Systems in the xPC Target User's Guide.
![]() | Basic Tutorial | Simulating the Model | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |