Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
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.

回答(1 个)

Arnav Mendiratta
Arnav Mendiratta 2017-3-20
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;

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by