You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to save plots so that they are in the same graph for different conditions
1 view (last 30 days)
Show older comments
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
Star Strider
on 25 Jul 2014
Edited: Star Strider
on 25 Jul 2014
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
Priya
on 25 Jul 2014
Thanks. Please find the code. I haven't posted everything, Just the conditions and the loop, thinking this is enough to explain.
Star Strider
on 25 Jul 2014
My pleasure! See my edited Answer under ‘Later, with posted code available...’
Star Strider
on 25 Jul 2014
Edited: Star Strider
on 25 Jul 2014
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
Star Strider
on 25 Jul 2014
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.
Priya
on 25 Jul 2014
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;
Star Strider
on 25 Jul 2014
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?
Star Strider
on 25 Jul 2014
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.
Priya
on 25 Jul 2014
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.
Star Strider
on 25 Jul 2014
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.
Star Strider
on 25 Jul 2014
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.
Priya
on 25 Jul 2014
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.
Star Strider
on 25 Jul 2014
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
Star Strider
on 25 Jul 2014
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.
Star Strider
on 25 Jul 2014
Edited: Star Strider
on 25 Jul 2014
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.
Star Strider
on 25 Jul 2014
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.
Star Strider
on 27 Jul 2014
Edited: Star Strider
on 6 Aug 2014
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.
Star Strider
on 27 Jul 2014
Edited: Star Strider
on 27 Jul 2014
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.
Star Strider
on 27 Jul 2014
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.
Star Strider
on 6 Aug 2014
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.
Priya
on 25 Aug 2014
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
Star Strider
on 25 Aug 2014
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.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)