0
votes

I have four matlab codes and each of them generates a plot how can be able to combine all the plots into one plot to show the transition of each?

2
We'll need a bit more information. Can you show a short example of how each file produces its plot? Can you describe how you would like the plots combined (all on the same axes, or on different axes?) - Alex
y5=[ku1;ku2;ku3;ku4;ku5;ku6;ku7;ku8;ku9]; x5=[5;10;15;20;25;30;35;40;45]; figure(3) plot(x5,y5,'ok'); So thats the plot from one matlab code now i want to combine all the four plots from different files into one coz the is a progression and i want to show that progression and its on the same axis same plot and same X and Y values. I cant combine all the four codes together since they all different txt files loaded in them, - user1407074

2 Answers

2
votes

If you want to plot multiple lines on the same figure you can use hold on For example:

plot(x1,y1,'ok');    
hold on
plot(x2,y2,'or');

If you are saying that they all form one single line then try concatenate your input vectors like this:

%Mock input
x1 = 0:9;
x2 = 10:19;
x3 - 20:29;
x4 = 30:39;
y1 = 2*x1 -20;
y2 = 2*x2 -20;
y3 = 2*x3 -20;
y4 = 2*x4 -20;
%Example of plotting concatenated vectors
plot( [x1;x2;x3;x4], [y1;y2;y3;y4]);
0
votes

If you want all four to be on the same figure (say figure 1) then you can do this:

%% In PlotCode1.m
figure(1)
hold on
...%your plotting code

%% In PlotCode2.m
figure(1)
hold on
...%your plotting code

And if you run each of your PlotCode.m files without closing or clearing figure 1 then all the lines will show up on the same figure.

Alternately, you can turn each of your different plotting files into functions that take in the figure number as an input. For instance:

   % In PlotCode1.m
   function PlotCode1(num)
     figure(num)
     hold on
     %Your plotting code

% In PlotCode2.m
  function PlotCode2(num)
     figure(num)
     hold on
     %Your plotting code

And now you can call each of these functions in one script:

 fignum = 2;
 PlotCode1(fignum)
 PlotCode2(fignum)

And now everything will plot on figure 2.