Reusing deleted figure handle
古いコメントを表示
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 件のコメント
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
2015 年 10 月 7 日
Walter Roberson
2015 年 10 月 7 日
You could set the "deleted" axis to 'Visible', 'off' so that it looked deleted but still existed.
TastyPastry
2015 年 10 月 7 日
編集済み: TastyPastry
2015 年 10 月 7 日
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.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!