MATLAB Examples

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 file is transfered from the target computer to the development computer, the data is read from the file and plotted.

Contents

Open, Build, and Download the Model

Open the model xpcFileLogging. 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(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','xpcFileLogging'));
end

Build the model and download to the target computer

  • Configure for a non-Verbose build.
  • Build and download application.
set_param('xpcFileLogging','RTWVerbose','off');
rtwbuild('xpcFileLogging');
### Starting Simulink Real-Time build procedure for model: xpcFileLogging
### Generated code for 'xpcFileLogging' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for model: xpcFileLogging
### Created MLDATX ..\xpcFileLogging.mldatx
### Looking for target: TargetPC1
### Download model onto target: TargetPC1
  • Close the model if we opened it.
if (mdlOpen)
  bdclose('xpcFileLogging');
end

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 Development Computer and Plot.

Attach to the target computer file system.

f=SimulinkRealTime.openFTP(tg);

Retrieve the file, open the file, read the data, close the file.

mget(f, 'fLogData.dat');
close(f);
h=fopen('fLogData.dat');
data=fread(h);
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');