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?

回答(1 个)

Amish
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!
  1 个评论
Leizer
Leizer 2024-4-30
Dear Amish,
Thank you very much for your attention.
The main idea is really to use it while simulating.
I saw that if I put the name of each one of the variables as an input from a sybsystem, it may works.
In this case, notice that the Output Scope do no recognize the names.
But if I open an internal sybsystem scope, tle scope legend uses the names that I defined.
So, using the same simulation, but looking under the subsystem:
Thus, I belive that if I can set the input variable names as defined in the workspace (<Names> at the original question), it may works. But I still do not know how to do it to verify if it works.
Thanks again !!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by