필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1 (최근 30일)
Bryan Skarda
Bryan Skarda 2017년 3월 11일
마감: MATLAB Answer Bot 2021년 8월 20일
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