Documentation Center

  • Trial Software
  • Product Updates

Data Logging With a File Scope

This example shows how to use an Simulink® Real-Time™ file scope to log signal data to the file system on the target computer. Signals are logged during model execution. At the end of the run, the data is read from the file, uploaded to the host computer and plotted.

Open, Build, and Download the Model

Open the model xpcFileLoggingxpcFileLogging. This model generates 20 sinusoids, each having a different amplitude. The signals are logged to the file fLogData.dat in the file system root directory on the target computer.

Note: Scopes of type 'target' are limited to 10 signals. However, scopes of type 'host' and 'file' can store a number of signals limited only by the hardware resources of the target computer.

% Open the model.
mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if all(~strcmp('xpcosc', systems))
  mdlOpen = 1;
  open_system('xpcFileLogging');
end

Build the model and download to the target computer

set_param('xpcFileLogging','RTWVerbose','off'); % Configure for a non-Verbose build.
rtwbuild('xpcFileLogging');                     % Build and download application.

% Close the model if we opened it.
if (mdlOpen)
  bdclose('xpcFileLogging');
end
### Starting Simulink Real-Time build procedure for model: xpcFileLogging
Warning: This model contains blocks that do not handle sample time
changes at runtime. To avoid incorrect results, only change
the sample time in the original model, then rebuild the model. 
### Successful completion of build procedure for model: xpcFileLogging
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

Run the Model

Using the Simulink Real-Time object variable, tg, start the model.

tg = SimulinkRealTime.target;

start(tg);

pause(5);
% Capture the target computer screen which displays the sinusoids in two target scopes.
tg.viewTargetScreen();

% Wait until the run is complete.
while strcmp(tg.Status, 'running')
  pause(0.05);
end

Upload Data to the Host Computer and Plot.

% Attach to the target computer file system.
f=xpctarget.fs;

% Open the file, read the data, close the file.
h=fopen(f,'fLogData.dat');
data=fread(f,h);
f.fclose(h);

% Unpack the data.
x=SimulinkRealTime.utils.getFileScopeData(data);

% Plot 1 second of the data.
figure; plot(x.data(:,21),x.data(:,1:20));
set(gca,'XLim',[0 1]); grid on;
xlabel('Time (sec)'); ylabel('Amplitude');

Was this topic helpful?