how to save plots so that they are in the same graph for different conditions

Hi,
I need to generate plots for four different conditions. So I will be having four curves on the whole. Now I get a plot for the first condition, and when I change the condition, I obviously get a different plot but I need to make the first plot remain in the same graph. Similarly for the third and fourth graphs (or conditions).
Please someone tell me how to do this. I have tried the 'hold on' command, nothing happens.
Thanks.

 Accepted Answer

See the documentation on subplot. It will give you as many plots as you want in the same figure.
If you want them all in the same set of axes, and hold did not work, post your code so we can see what you did and what the problem is.
Later, with posted code available...
One problem is that you’re not telling it to plot the correct vector. See if changing the plot command to:
plot(s_x_index, f_x_save, 'x')
improves things.

34 Comments

Thanks. Please find the code. I haven't posted everything, Just the conditions and the loop, thinking this is enough to explain.
My pleasure! See my edited Answer under ‘Later, with posted code available...’
You’re only plotting one plot. That’s all you can do with the code you posted. If randomindex has something to do with it, you may need to create f_x_save as a matrix using randomindex as well:
f_x_save(randomindex,i) = f_x;
and then loop through f_x_save incrementing randomindex to get all four plots:
figure(5)
for k = 1:size(f_x_save,1)
plot(s_x_index, f_x_save(k,:), 'x')
hold on
end
hold off
Just use this line, as I suggested in my previous Comment:
f_x_save(randomindex,i) = f_x;
There is no reason to save f_x separately if you are saving it in the f_x_save matrix.
Yes I did. But on doing so, It gives
Subscripted assignment dimension mismatch.
Error in test_contact_ellipse (line 402)
f_x_save(randomIndex,i) = f_x;
I don’t have all the information to run your code. What size is f_x? Is it a scalar (that I assumed) or a vector?
That’s the problem.
Change the f_x_save line to:
f_x_save(randomIndex,:) = f_x;
That should work with the rest of the code I posted as well.
Thanks it works partially now. But I need all 4 plots to use the same axis limits.
The problem now is, its changing the axis by itself which in turn affects the other plots.
The easiest way to do that is to use the axis function. Set the axis limits to the values that accommodate all your plot data.
According to the legend, all four are actually plotted.
Change the plotting loop to:
figure(5)
hold on
for k = 1:size(f_x_save,1)
plot(s_x_index, f_x_save(k,:), 'x')
end
hold off
See if that works.
Not again.Yes it plots all the four, but the thing is when it plots for one condition the other is as shown at zero.
OK. Abandon the loop and just go with this but outside (after) the loop:
figure(5)
plot(s_x_index, f_x_save, 'x')
grid
I'm afraid not! It's still the same.
I’m out of ideas. There must be something in your code that calculates them that is the problem.
Just after the f_x line, put:
minmax(randomindex,:) = [min(f_x) max(f_x)];
This is a way of determining where the problem is. If those values are different from the plotted values, the problem may be in the f_x_save matrix somewhere.
The values are same. No problem about that.
OK, now do:
minmax2 = [min(f_x_save,[],2) max(f_x_save,[],2)]
after the loop and see what that is. It should be the same as minmax. If it isn’t, the problem is in f_x_save.
If they’re the same and have the appropriate values, I have no idea why the plot is not working as you believe it should. I would use:
figure(5)
plot(s_x_index, f_x_save, 'x')
grid
after the loop and without the axis statement and see how the plot looks.
Thanks anyway for your suggestions.
Please post the entirety of your new code. I still have the old data file, but the code you posted isn’t what you are currently running.
EDIT — Turns out you simply need a loop with randomIndex:
[PLOT REMOVED]
I also attached your (slightly edited) test_contact_ellipse.m script file.
——————————
NOTE (06 Aug 2014) — After several weeks (in some instances) it turns out that OP’s posted code and data in this and other Questions are proprietary. At OP’s request, I have removed them from this Comment.
Please see my previous comment. Is it what you want?
EDIT —
For clarity, this line becomes Line #307 in the code you posted in your ‘25 Jul 2014 at 12:14’ Comment:
for randomIndex = 1:4 % START ‘randomIndex’ LOOP
with this line then becoming Line #428:
end % END ‘randomIndex’ LOOP
Those were the only changes I made.
My pleasure!
Your entire plotting loop becomes:
cs = {'^b'; 'pg'; 'sr'; 'vk'};
figure(5);
% for k = 1:size(f_x_save,1)
for k = 1:4
plot(s_x_index, f_x_save(k,:), cs{k} )
xlabel('Longitudinal creepage');ylabel('coefficient of adhesion')
hold on;
end
axis([0 0.02 0 0.6])
hold off
legend('Dry','Wet','Low','VLow')
Change the colours and symbols to your choices. They are defined in the ‘cs’ (colour-symbol) cell array. I chose a cell in the event you want to change the lengths of any of the elements, such as to connect some or all of the symbols in any set with lines, for instance '--b^' to connect the first set with a dashed blue line. The cell array will accomodate different sizes of the individual entries.
Great help indeed. Thank you so much.
Hi,
One kind request, could you please remove the plot and m-file you have attached in the comment dated 27 Jul 2014 at 18:42 . Is it possible to do?
Sorry for the inconvenience.
Thanks in advance.
I can remove them, but would prefer not to in case others have a similar problem and are looking for a solution.
I am moved to ask the reason for your request. There are nothing with respect to either of them that I would consider in any way offensive, the one criterion I would use to remove them immediately.
I'm personally not feeling comfortable in keeping the whole code in web for a long time as this file reflects in google search and this is a piece of work which is related to my research. Please do the needful.
Hi StarStrider,
Could you please find the link below, and help me. I have moved onto a different stage of this program now.
Can you please help me?
Thanks
I would like to, but I haven’t used Simulink in a while. It would take me at least several days if not weeks to become proficient enough in it again to be able to solve problems regarding Simulink programming. Be sure to put Simulink in the ‘Products’ tag window so that people proficient in Simulink will see it.
Thanks, but I'm not able to upload .slx file.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Products

Tags

Asked:

on 25 Jul 2014

Commented:

on 25 Aug 2014

Community Treasure Hunt

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

Start Hunting!