How to set the legends using a Simulink plot, when the input data (a matrix) comes from the workspace
7 次查看(过去 30 天)
显示 更早的评论
% 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
%==================
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
Conecting this source to a scope, it is possible to see the simulation result
As we have the name of each variable:
How to use this Names at the Simulink plot legend?
0 个评论
回答(1 个)
Amish
2024-4-30
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!