Reputation: 179
I have in Simulink a Scope with multiplexor block Mux (i want to draw multiples wavaforms in one graph). After simulation i need to export it in defined form (background color, lines width, etc.) to eps/pdf and png files.
Actual problem:
My dream:
The final state is to fulfill my dream.
My m file:
% 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
Thank you.
Upvotes: 0
Views: 244
Reputation: 10375
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).
Upvotes: 1