I have a GUI which displays several axes (array of axes) that the user can switch between to plot on. Here's what the relevant portion looks like:
The issue I'm running in to is that I have a delete button (not shown) which deletes an axes using delete(handle), but I'd like to implement an undo button (not shown). When an axes is deleted, the code stores it and the index in the array in case the user undoes the operation. During the undo, the deleted axes is restored to the previous location in the array. There's a line of code:
axes(plotWindow.plot(lastAxesInd));
%plotWindow is the GUI
%plotWindow.plot is the array of plots
%lastAxesInd is the index of the axes which was deleted. it is being inserted back to the same location
which is meant to switch the focus of the GUI to the restored axes. This causes Matlab to crash, which I'm assuming is because I'm trying to switch focus to a handle which has been deleted.
What can I do to go around this issue? I'd like to not have to append a new axes to the end of the array, since I use the axes' indices as a way to track which plot the user is plotting on, shown above.

5 件のコメント

Adam
Adam 2015 年 10 月 7 日
編集済み: Adam 2015 年 10 月 7 日
Can you be more specific than "causes Matlab to crash"? Do you mean you just get the window telling you Matlab must close or do you get an error message (if so please post it as it usually points to what the error actually is without having to guess).
In terms of the problem, I assume the user can only undo the last deletion rather than multiple undos all the way back to the first axes they ever deleted? Also I assume that when an axes (e.g. index 5) is deleted then axes 6 becomes the 5th one (as the user sees it in your UI), but then returns to being index 6 if they undo the deletion of index 5?
Personally I'd probably be tempted to leave the axes in the array when it gets deleted and just store its index in another 'deletedAxis' variable. Then keep a mapping of user axis indices to actual axes array indices so that you can just re-map the user indices when an axis gets deleted.
If you want to keep a minimum number of axes around then when the user does another delete operation you can permanently delete the previously deleted one (whose index you stored, but which was still in the array) if the user is not allowed to undo more than the last delete, then handle the new deletion in the same way.
That way you remove the axes that will never be restored, but keep the last deleted one so that restoring it is a trivial case of remapping the user indices and axes array indices rather than having to reintroduce the deleted axes back into the array.
TastyPastry
TastyPastry 2015 年 10 月 7 日
Matlab terminates, i.e. crashes to desktop, popup asks to send logs.
And yes, the user can only undo the last deletion. If there are 7 plots and the user deletes number 4, then plot 5 becomes 4, 6 becomes 5, etc. If the user undoes the deletion, the deleted axes returns to its original location.
So what you're saying is that whenever I restore the deleted axes, I should tack it on to the end of the plotWindow.plot array. Then, in the example with 7 axes, the restored 4th axes would actually go in plotWindow.plot(8), and the vector would show [1 2 3 8 5 6 7].
Walter Roberson
Walter Roberson 2015 年 10 月 7 日
You could set the "deleted" axis to 'Visible', 'off' so that it looked deleted but still existed.
TastyPastry
TastyPastry 2015 年 10 月 7 日
編集済み: TastyPastry 2015 年 10 月 7 日
Yeah I figured I'm just going to stick with setting the "deleted" axes to 'visible','off', even though that leaves a trail of deleted axes in my array if the user deletes a ton of them. This also makes restoring a lot easier since I just need to change 'visible','on' and refocus the GUI on the restored axes.
Adam
Adam 2015 年 10 月 7 日
I don't really know what the overhead is of non-visualised axes, though it depends more what is plotted on them since the data will remain in memory.
Permanently deleting all previously deleted axes is easy though. As I said, so long so long as you keep the index of the one the user just selected to delete you can hide that one, but delete the previously "hidden" one because it can now never be restored by the user. Just don't physically delete the very latest one that might be restored.

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2015 年 10 月 7 日
編集済み: Stephen23 2015 年 10 月 7 日

0 投票

How about simply keeping all of the axes (no deleting!) and switching the axes' visibility property on and off? Then all of your axes handles remain valid for as long as the axes exist, even if they are not visible.

2 件のコメント

TastyPastry
TastyPastry 2015 年 10 月 7 日
I did do some research beforehand and there were several solutions where the user changed visibility, but I'd like for any deleted axes to be removed from the plotWindow.plot array. I have the part of the UI where there's a box between "Previous" and "Next" (shown above), which tracks the index of the plot the user is working on. If I were to delete say, plot 4 of 7 and make it not visible, then when the user presses "Next" on plot 3, they would see a blank plotting area because the figure exists, but is invisible. I'm thinking I have to create a map which tracks which figures are not deleted.
TastyPastry
TastyPastry 2015 年 10 月 7 日
On second thought, it's easier to restore the axes if I just keep all the created ones. It'll create a trail of unused axes if the user just deletes stuff a lot. Would it make that big of a difference in performance/memory usage if I also deleted axes not covered by the undo button? I could have the program clean up inactive axes by deleting them.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2015 年 10 月 7 日

コメント済み:

2015 年 10 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by