Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Error in iterative graphs if I close figure window between graphing requests

1 vue (au cours des 30 derniers jours)
Bryan Skarda
Bryan Skarda le 11 Mar 2017
Clôturé : MATLAB Answer Bot le 20 Août 2021
I'm writing a program that performs some calculations and asks the user if they would like to see a graph. If they say yes, I graph it; it always works the first time. The program then runs through a second set of calculations using a different seed number and again asks the user if they wish to see a graph. The problem arises if the user has closed the figure window between the first and second request to see a graph. If they haven't closed it, all works well but if they have, it throws an error. The only thing I've found to fix it is adding a short pause right before I graph. That seems to allow Matlab time to catch up and open a new figure window before attempting to display the graph but that makes no sense to me. Code is below
if Graph == 'y'
r = 0.1:.1:100;
dp = delta_p(r); % Calls a function delta_p that takes r vector, runs large calculations and returns
pause(.3); % I added this to make it work
semilogy(r,dp);
grid on;
saveas(figure(1),'Output_Plot.jpg');
end
Thoughts? Thank you.

Réponses (1)

Arnav Mendiratta
Arnav Mendiratta le 20 Mar 2017
The reason that you get an error is because you are trying to modify the handle that has already been deleted (when the user closes the figure). When you ask whether a graph should be plotted or not, you can instead use the 'Visible' property of the figure handle to display (or not display) the graph based on user input. Since you are using 'semilogy' which outputs a 'Line' object, this means the figure handle of this graph would be the second parent of the object. First of all, output the handle of semilogy so you can modify this:
lineseries = semilogy(r,dp)
Then, if you want to display the graph, you can just mark it as visible by including the following line of code:
lineseries.Parent.Parent.Visible = 'on'
If the user does not want to display the graph, you can simply turn off the visibility like this:
lineseries.Parent.Parent.Visible = 'off'
Further, when you are modifying the data in the iterations within your code, instead of adding a new graph every time, you can consider modifying the data in X- and Y-axis of the Line object. For example:
lineseries.Xdata = newXData;
lineseries.YData = newYData;

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by