How to set the legends using a Simulink plot, when the input data (a matrix) comes from the workspace
7 views (last 30 days)
Show older comments
% In the workspace, create a matrix T to be used by Simulink
T(1:5,1)=0:4;
T(1:5,2)=2;
T(1:5,3)=3;
% The T first column represents the time
% The T second column represents the fist variable values
% The T third column represents the second variable values
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1680381/image.png)
%==================
Now, using the Simulink
Selecting the source "From Workspace"
Setting T as the parameters Data
Setting sample time as -1
Setting to "Holdin Final value" from output, after final data
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1680386/image.png)
Conecting this source to a scope, it is possible to see the simulation result
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1680391/image.png)
As we have the name of each variable:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1680396/image.png)
How to use this Names at the Simulink plot legend?
0 Comments
Answers (1)
Amish
on 30 Apr 2024
Hi Leizer ,
The Simulink "Scope" block does not directly support adding legends based on matrix data input. However, there are a few workarounds that you can try to set legends in a Simulink plot.
Using MATLAB Function Block for Custom Plotting : Connect a "MATLAB Function" Block to the "From Workspace" block and write a custom function for plotting. The function may look like:
function plotData(u)
% Assuming u is the matrix T imported from the workspace
time = u(:,1);
data1 = u(:,2);
data2 = u(:,3);
% plot
plot(time, data1, time, data2);
legend('Variable 1', 'Variable 2');
xlabel('Time');
ylabel('Value');
title('Simulation Results');
end
Post-Processing with MATLAB Script: This approch will use a MATLAB script for plotting and adding legends after the simulation completes. Log the plot after simulation and store in your workspace ("To Workspace" block). After the simulation, use the logged data in MATLAB to create plots.
Here are some documentation links for your reference:
Hope this helps!
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!