Issues with graph plotting on to of itself

I appear to have a problem comprehending the hold situation for figures, as the output from export_fig and the figures that flash up are a mixture of differing elements; it is not keeping the graphs separate either between the two requested in the code or as the code loops. I am getting the right filenames and number of files but the graphs are all very confused.
for k = 1:numel(C)
dtF = sprintf('dt%s.csv',C{k});
xyF = sprintf('xy%s.csv',C{k});
vxF = sprintf('vx%s.csv',C{k});
dt = readtable(fullfile(D,dtF));
xy = readtable(fullfile(D,xyF));
vx = readtable(fullfile(D,vxF));
axis equal
XAxisLocation = 'origin';
YAxisLocation = 'origin';
hold
scatter(xy.x,xy.y, '.');
plot(dt.x1, dt.y1,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 8);
plot(dt.x2, dt.y2,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 5);
title('Particle path over time')
xlabel('x')
ylabel('y')
export_fig(sprintf('xy%s.png', C{k}));
hold
hold
XAxisLocation = 'bottom';
YAxisLocation = 'right';
scatter(vx.x,vx.vx, '.');
title('Velocity of particle in the x direction when y = 0 and x < 0')
xlabel('x')
ylabel('Vx')
export_fig(sprintf('vx%s.png', C{k}));
hold
end

 Réponse acceptée

Star Strider
Star Strider le 25 Août 2020
I cannot run your code, and I have no certain idea what the problem is.
Experiment with creating separate figures, and change the orders of some of the statements:
figure
hold on
scatter(xy.x,xy.y, '.');
plot(dt.x1, dt.y1,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 8);
plot(dt.x2, dt.y2,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 5);
title('Particle path over time')
xlabel('x')
ylabel('y')
axis equal
XAxisLocation = 'origin';
YAxisLocation = 'origin';
export_fig(sprintf('xy%s.png', C{k}));
hold off
figure
hold on
scatter(vx.x,vx.vx, '.');
title('Velocity of particle in the x direction when y = 0 and x < 0')
xlabel('x')
ylabel('Vx')
XAxisLocation = 'bottom';
YAxisLocation = 'right';
export_fig(sprintf('vx%s.png', C{k}));
hold off
Specifying hold on and hold off may not specifically be necessary, however it does clarify the hold state.
I cannot run your code, so I obviously cannot test this. It will work, however I cannot guarantee that it will solve your problem. If it doesn’t, I’ll delete this Answer.
.

4 commentaires

I tried it but it did not solve the issue, whilst moving the other elements meant they did not occur (moving the Axis Location commands post construct means they don't affect the chart; to do so requires that you make it understand it is working with the current graph/chart).
My total code structure (with the changes you recommended and the amendments required to make it function as before included)...
D = '/Users/garynewport/Desktop/Programming/Restricted 3 Body Problem/Restricted 3 Body Problem';
C = {'09_4','11_4','12_4','13_4','09_6','10_6','11_6','12_6','09_8','10_8','11_8','12_8','13_8'};
xyG = figure();
vxG = figure();
for k = 1:numel(C)
dtF = sprintf('dt%s.csv',C{k});
xyF = sprintf('xy%s.csv',C{k});
vxF = sprintf('vx%s.csv',C{k});
dt = readtable(fullfile(D,dtF));
xy = readtable(fullfile(D,xyF));
vx = readtable(fullfile(D,vxF));
hold on
scatter(xy.x,xy.y, '.');
plot(dt.x1, dt.y1,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 8);
plot(dt.x2, dt.y2,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 5);
title('Particle path over time')
axis equal
ax = gca;
ax.XLabel.String = 'x';
ax.YLabel.String = 'y';
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
export_fig(sprintf('xy%s.png', C{k}));
hold off
hold on
scatter(vx.x,vx.vx, '.');
title('Velocity of particle in the x direction when y = 0 and x < 0')
ax = gca;
ax.XLabel.String = 'x';
ax.YLabel.String = 'Vx';
ax.XAxisLocation = 'bottom';
ax.YAxisLocation = 'right';
export_fig(sprintf('vx%s.png', C{k}));
hold off
end
However, it still generates graph on top of graph.
I have attached a selection of the data files if you wished to get an idea of the issue and a possible resolution.
Star Strider
Star Strider le 25 Août 2020
See if adding the figure calls helps. You can clear each figure after you save it using the clf function.
And that has it solved!
Actually, in reading about clf (which created multiple figure references within the workspace) I realised that what I needed to do was delete the figure after it was created; so read about close.
This now works and I have the png files I need.
Of course I now realise that I need to be able to zoom in on the xy graphs, so, will need to also save them as .fig files; but that is an easy step to take.
My code is now...
D = '/Users/garynewport/Desktop/Programming/Restricted 3 Body Problem/Restricted 3 Body Problem';
C = {'09_4','11_4','12_4','13_4','09_6','10_6','11_6','12_6','09_8','10_8','11_8','12_8','13_8'};
for k = 1:numel(C)
dtF = sprintf('dt%s.csv',C{k});
xyF = sprintf('xy%s.csv',C{k});
vxF = sprintf('vx%s.csv',C{k});
dt = readtable(fullfile(D,dtF));
xy = readtable(fullfile(D,xyF));
vx = readtable(fullfile(D,vxF));
xyG = figure();
hold on
scatter(xy.x,xy.y, '.');
plot(dt.x1, dt.y1,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 8);
plot(dt.x2, dt.y2,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 5);
title('Particle path over time')
axis equal
ax = gca;
ax.XLabel.String = 'x';
ax.YLabel.String = 'y';
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
export_fig(sprintf('xy%s.png', C{k}));
hold off
close
vxG = figure();
hold on
scatter(vx.x,vx.vx, '.');
title('Velocity of particle in the x direction when y = 0 and x < 0')
ax = gca;
ax.XLabel.String = 'x';
ax.YLabel.String = 'Vx';
ax.XAxisLocation = 'bottom';
ax.YAxisLocation = 'right';
export_fig(sprintf('vx%s.png', C{k}));
hold off
close
end
Thank you for allof your help.
Star Strider
Star Strider le 25 Août 2020
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by