What is the logic in the way plots are handled in Live Scripts?
6 views (last 30 days)
Show older comments
Henning Søgaard
on 21 Nov 2023
Answered: Steven Lord
on 21 Nov 2023
Example: I have the following code in a Live Script:
fplot(@sqrt,[0 10]), legend('sqrt')
subplot(2,1,1)
fplot(@sin,[0 2*pi]), legend('sin')
subplot(2,1,2)
fplot(@cos,[0 2*pi]), legend('cos')
fplot(@log,[0.1 10]), legend('log')
subplot(2,1,1)
fplot(@tan,[0 2*pi]), legend('tan')
subplot(2,1,2)
fplot(@cot,[0 2*pi]), ylim([-15 15]), legend('cot')
I expect the code to produce four figures (of which two of them are containing subplots), but I only get two figures (the first one and the last one):
What is the logic behind that?
I know that I can get my four figures by inserting the command figure as I have done in the following code, but why do I need to do something like this?
fplot(@sqrt,[0 10]), legend('sqrt')
figure
subplot(2,1,1)
fplot(@sin,[0 2*pi]), legend('sin')
subplot(2,1,2)
fplot(@cos,[0 2*pi]), legend('cos')
figure
fplot(@log,[0.1 10]), legend('log')
figure
subplot(2,1,1)
fplot(@tan,[0 2*pi]), legend('tan')
subplot(2,1,2)
fplot(@cot,[0 2*pi]), ylim([-15 15]), legend('cot')
0 Comments
Accepted Answer
Steven Lord
on 21 Nov 2023
From the documentation for the subplot function: "subplot(m,n,p) divides the current figure into an m-by-n grid and creates axes in the position specified by p. MATLAB® numbers subplot positions by row. The first subplot is the first column of the first row, the second subplot is the second column of the first row, and so on. If axes exist in the specified position, then this command makes the axes the current axes."
So your second subplot(2, 1, 1) command makes that the current axes. It does not, as it sounds like you expected it to, create another figure with an axes on it. To do that you would need to call figure. Alternately if you want the sine and tangent curves to be on the same axes, call hold on before calling fplot the second time on the subplot(2, 1, 1) axes.
0 Comments
More Answers (0)
See Also
Categories
Find more on Subplots 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!