用于创建和导出绘图的Matlab脚本

时间:2013-03-23 10:37:30

标签: matlab plot export

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

实际问题:

  • 图例中的颜色不正确。

我的梦想

  • 在Simulink中开始模拟(单击F5开始)
  • 然后调用我自己的函数(脚本)来导出它(例如 set_and_export(标签x,标签y,图例波1,图例波2,..,图例波x)

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

我的档案:

% 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

谢谢。

1 个答案:

答案 0 :(得分:1)

问题是同时使用legendhold on:因为您使用hold on,所以MATLAB在绘制新图之前不会清除旧图。但它不会存储legend以前的图表信息。您需要手动执行此操作。

这是一些代码(未经测试,目前无法访问MATLAB):

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{:});

所以:将plot返回的句柄存储在向量中,并将其传递给legend(循环后需要调用)。