0

我在 Simulink 中有一个带有多路复用器块 Mux 的 Scope(我想在一个图中绘制多个波形)。模拟后,我需要以定义的形式(背景颜色、线条宽度等)将其导出为 eps/pdf 和 png 文件。

实际问题:

  • 图例中的颜色不正确。

我的梦想

  • 在 Simulink 中开始仿真(点击 F5 开始)
  • 然后调用我自己的函数(脚本)来导出它(例如set_and_export(label x, label y, legend wave 1, legend wave 2, .. ,legend wave x)

最后的状态是实现我的梦想。

我的 m 文件:

% Get the data from Simulink
% First column is the time signal
% in Scope in Simulink : Array with time
[nothing, NumOfSgns] = size(ScopeData)
time = ScopeData(:,1);

% Plot all signals
hold on
for j=0:NumOfSgns-2,
    graph=plot(time,ScopeData(:,2+j:end),'Color', rand(1,3));

    % Signals description and position of the legend
    legend('firs wave form','next wave form','Location','SouthEast');
end
hold off

谢谢你。

4

1 回答 1

1

The problem is using both legend and hold on: Because you use hold on, MATLAB doesn't clear the old plot before drawing the new. But it doesn't store the previous plots' information for legend. You need to do this manually.

Here's some code (untested, don't have access to MATLAB at the moment):

titles = {'A', 'B', 'C', 'D'};
handles = zeros(1, length(titles));
figure;
hold on;
for i = 1 : length(titles)
    handles(i) = plot(1 : 10, rand(1, 10), 'Color', rand(1, 3));
end
legend(handles, titles{:});

So: Store the handles returned by plot in a vector and pass it to legend (which you need to call after the loop).

于 2013-03-23T12:46:49.077 回答