How to Log Signals in Simulink: Step-by-Step Guide
To log signals in Simulink, right-click the signal line and select
Log Selected Signals. Then run the simulation, and the logged data will be available in the Simulink.SimulationOutput object for analysis.Syntax
In Simulink, logging a signal involves enabling the logging property on the signal line. The main steps are:
signal.Line: The signal line you want to log.signal.LoggingInfo: Property that stores logging settings.set_param: Function to programmatically enable logging.sim: Function to run the simulation and collect logged data.
Example syntax to enable logging programmatically:
matlab
set_param('model_name/SignalName','DataLogging','on'); out = sim('model_name'); loggedData = out.logsout.getElement('SignalName').Values;
Example
This example shows how to log a signal named Voltage in a Simulink model called simple_circuit. It enables logging, runs the simulation, and extracts the logged data for plotting.
matlab
model = 'simple_circuit'; load_system(model); % Enable logging on the signal named 'Voltage' set_param([model '/Voltage'], 'DataLogging', 'on'); % Run the simulation out = sim(model); % Extract logged signal data voltageData = out.logsout.getElement('Voltage').Values; % Plot the logged signal plot(voltageData.Time, voltageData.Data); title('Logged Voltage Signal'); xlabel('Time (s)'); ylabel('Voltage (V)');
Output
A plot window showing the Voltage signal over time after simulation.
Common Pitfalls
Common mistakes when logging signals in Simulink include:
- Not enabling logging on the correct signal line.
- Forgetting to run the simulation after enabling logging.
- Trying to access logged data before simulation finishes.
- Using incorrect signal names when extracting data.
Example of wrong and right ways:
matlab
% Wrong: Trying to get logged data without enabling logging % out = sim('model'); % data = out.logsout.getElement('Signal').Values; % This will error if logging is off % Right: Enable logging first set_param('model/Signal', 'DataLogging', 'on'); out = sim('model'); data = out.logsout.getElement('Signal').Values;
Quick Reference
| Action | Command / Step |
|---|---|
| Enable signal logging | Right-click signal line > Log Selected Signals OR set_param('model/Signal','DataLogging','on') |
| Run simulation | out = sim('model') |
| Access logged data | out.logsout.getElement('SignalName').Values |
| Plot logged data | plot(data.Time, data.Data) |
Key Takeaways
Enable logging on the signal line before running the simulation.
Use the simulation output object to access logged signal data.
Always verify the signal name matches when extracting logged data.
Run the simulation after enabling logging to collect data.
Use plots to visualize logged signals for analysis.