0

I am trying to plot figures that have a point inside it. I want to add a new point to the figure at each 10 steps (or any constant rate).For example, if I have 50 steps then I should have 50 figures. Figures from 1 to 10 will have one point, figures from 11 to 20 will have two points, and figures from 21 to 30 will have three points, and so on. I did that in the below code but, unfortunately, it gave me only one point for all figures.

clc;
clear;
Current_Path=pwd;
cd (Current_Path)
mkdir('Photos','part1')
pridir = 'Photos\part';
R=rand(1,50);
Y=rand(1,50);
for i=1:50
    figure
    for jj = 1:floor((i-1)/10)+1
        if jj<=1
            plot (R(i),Y(i),'*r');
            printto = sprintf('%s%d\\Motion%03d',pridir,1,i);
            print('-djpeg90',printto)
            close(gcf);
            hold on
        else
            R(i)=R(i-((jj-1)*10));
            Y(i)=Y(i-((jj-1)*10));
            plot (R(i),Y(i),'*r');
            printto = sprintf('%s%d\\Motion%03d',pridir,1,i);
            print('-djpeg90',printto)
            close(gcf);
            hold on
        end
    end
    hold off
end
Adriaan
  • 17,741
  • 7
  • 42
  • 75
user6052232
  • 169
  • 1
  • 10

1 Answers1

1

Your code has two mains problems:

  • You're closing the figure after each plot. Move the close(clf) to the location your hold off currently is to avoid closing the figure after each plotted point.
  • You pridir call is missing a 1 in the part1, which causes MATLAB to not save the figure since there is no directory part.

To prettify you could make a couple of additional changes. hold on holds a figure until you call hold off. In your case you can omit the hold off completely, as you close the figure after the loops. Also, consider using ii as a variable (just as your jj) since i is the imaginary unit.

Updated code

Current_Path=pwd;
cd (Current_Path)
mkdir('Photos','part1')
pridir = 'Photos\part1'; % change to 'part1'
R=rand(1,50);
Y=rand(1,50);
for ii=1:50 % change to ii
    figure
    hold on % move hold on here
    for jj = 1:floor((ii-1)/10)+1
        if jj<=1
            plot (R(ii),Y(ii),'*r'); % remove close(gcf) and print calls
        else
            R(ii)=R(ii-((jj-1)*10));
            Y(ii)=Y(ii-((jj-1)*10));
            plot (R(ii),Y(ii),'*r'); % remove close and print 
        end
    end
    printto = sprintf('%s%d\\Motion%03d',pridir,1,ii); 
    print('-djpeg90',printto) % move print outside the inner loop
    close(gcf); % finally close the figure after saving
end
Community
  • 1
  • 1
Adriaan
  • 17,741
  • 7
  • 42
  • 75