MATLAB Answers

Set up graph before going into for loop, then refresh data in plot after each iteration

59 views (last 30 days)
monkey_matlab
monkey_matlab on 3 Apr 2016
Edited: Kuifeng on 3 Apr 2016
In this program, I wanted to know if it is possible to update just the data points during the for loop, but keep the graph setup the same throughout the for loop process. I tried setting up the graph at first, like the axis, labels and legend ( I had a problem with the legend), then go into the for loop and just refresh the data. In the program below, after each iteration, the old data remains and the graph just keep adding the new data points without getting rid of the old ones. I tried clf, but that command clears the whole graph and I would have to start back from the beginning to set back up the graph.
Is it possible to just refresh the data points after each iteration, or do I have to draw the graph over each time?
Here is the code that I have so far:
N = 10;
h2 = [];
h3 = [];
%%Plot of Original and OMP Signals
figure(1); clf;
axis([1 N -3 3]);
h=xlabel('index [1 to m]');
legend([h2,h3],{'x-Original','x-OMP'})
set(h,'FontSize',14);
ylabel('Amplitude');
set(h,'FontSize',14);
set(gca,'FontSize',14);
Iter = 5;
for ii = 1:1:Iter
if ii == 1 % This is done so as to plot the legend only on the first iteration
A = rand(1,N);
C = rand(1,N);
B = 1:1:N;
pause(1);
%h1 = plot(A);
hold on;
h2 = plot(B,A,'.r');
set(h2,'MarkerSize',15);
h3 = plot(B,C,'.b');
set(h3,'MarkerSize',7);
legend([h2,h3],{'x-Original','x-OMP'});
else
A = rand(1,10);
B = 1:1:N;
C = rand(1,N);
pause(1);
%h1 = plot(A);
hold on;
h2 = plot(B,A,'.r');
set(h2,'MarkerSize',15);
h3 = plot(B,C,'.b');
set(h3,'MarkerSize',7);
end
end

Products

Community Treasure Hunt

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

Start Hunting!

Translated by