Plotting multiple graph in same graph

I am trying to plot all the graphs of each txt file in one graph. I am using this code but it is plotting them separately. Here is my code..
%Creating a continuous loop which will only stop after reaching j=size(READ,2)
c = 0;
%graphics_toolkit gnuplot
figure('visible','off');hold on
%This lopp will take column 1,3,5,7...............
for j = 1: 2 : size(READ,2)
c = c+1;
clf
x=READ(:,j);
y=READ(:,j+1);
plot(x_0,y_0)
end
hold off
end

 Accepted Answer

...
for j = 1: 2 : size(READ,2)
c = c+1;
clf
...
Well, you wipe out everything up to here--if the point is to add to the given figure, this defeats that.
clf is almost never needed in script or function; as a general rule forget it and cla and friends even exist; only when you discover a need go and dust them off...

8 Comments

Can you please edit my code and post it in the comment? Thanks for your reply.
madhan ravi
madhan ravi on 12 Sep 2018
Edited: madhan ravi on 12 Sep 2018
just remove clf from you code @Atta
How ever is it possible within this loop once i will plot them separately and in the same time plot them as one graph? If so how can i do that without repeating the same loop again?
Thanks
You can only do what your code "says" to do...if you want two separate figures (or more than two, perhaps, if you don't want to destroy the one-at-a-time cases until you have a chance to look at them at your leisure), besides the one figure with all plots on one axes you'll have to have create those additional figures explicitly.
Actually i have two directory where in one i need all the separate graphs and in another i need to have them in one graph. Is it possible?

"Anything" is possible; you just have to clearly define what it is that is wanted and write logic to incorporate those items.

I don't know what "two director[ies] where in one i need all the separate graphs and in another i need to have them in one graph" means. How are directories (a file structure/storage concept) and figures/graphs related to each other?

Mr. 206
Mr. 206 on 13 Sep 2018
Edited: Mr. 206 on 13 Sep 2018

Using this piece of code i am saving all the separate graph in "Test_output_01" folder.

%Now saving the file in a certain directory        
            fileName = ['/home/ali/Master_Thesis_Ali/Test_output_01/', initialFileName, '.png'];        
            print(fileName, '-dpngcairo');

I just dont want to repeat the whole

for j = 1:2:size(READ,2) 
loop again just to plot them together. 

So what i want is to tweak the loop a bit to plot & save them separately in "Test_output_01" folder and together in folder "Test_output_02".

Accept my apology if i can't make myself clear. And thanks for your help.

Inside the loop you need both directory names and to write to each after you've drawn the specific figure that belongs in that subdirectory.

You may find structuring the code to do this simpler if you encapsulate the existing code into its own function but with another couple of inputs; a flag as to whether to create multiple plots on a single figure (hold on) or to create a figure for each data set (hold off) and the name of the directory to use for output.

Then you could simply call that function from an outer loop with the control variables as wanted and not have to try to mung internally to the function itself.

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 12 Sep 2018

Commented:

dpb
on 13 Sep 2018

Community Treasure Hunt

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

Start Hunting!