How can I maintain an array of rectangle objects that don't necessarily all exist at the same time?

2 次查看(过去 30 天)
tldr version:
  • How can I create an empty array of rectangles at the start of my code? (I want to initialize handles.rect = 21x1 rectangle objects without drawing any of them)
  • How can I query if a specific rectangle has been created? (has handles.rect(3, 1) been drawn already?)
  • How can I delete a rectangle from the figure but still leave it a place in the array? (delete(handles.rect(3, 1)) from figure, but size(handles.rect) = [21, 1], preferably handles.rect(3,1) would go back to the same value it has after initializing as in the first bullet point)
Detailed version:
I have a figure containing 30+ rectangles (drawn with the built-in rectangle() function), and I want to let the user identify up to 21 of them (and color code them appropriately). My current solution looks like:
% This function colors in rectangle One. If no valid rectangle is selected, no drawing occurs.
% If rectangle one was defined previously, remove it from the figure before drawing the new instance of rectangle one
function drawRectOne(hObject, ~)
handles = guidata(hObject);
if (handles.userClass.pres == true % Determine if rectangle one is already present
delete(refHandles.rectOne); % Remove it from figure if it is
end
% My function that determines which rectangle the user clicks on; relevant data is saved inside handles
drawRect = drawRectGeneric(handles, 1) % drawRect is true/false to indicate if a valid rectangle was chosen
if drawRect % Add colored rectangle to figure over the selected rectangle
refHandles.rectOne = rectangle('Position', [handles.userClass.X, handles.userClass.Y, handles.userClass.width, handles.userClass.height], 'FaceColor', handles.colOne);
end
guidata(hObject, handles);
end
Due to the fact that 21 rectangles can be selected (each from a different pushbutton), and that there will be additional pushbuttons with the same purpose in another figure of the gui, I'm trying to make drawRectGeneric as generic as possible so that each pushbutton is only 3 lines of code that passes the necessary info to drawRectGeneric.
However, if I try to pass the colored rectangle handle:
drawRectGeneric(handles, 1, refHandles.rectOne);
Then I get an error the first time I press the pushbutton: "Reference to non-existent field 'rectOne'". I know this is because refHandles.rectOne does not exist until the first valid rectangle is chosen, but I can't figure out a way to fix it.

采纳的回答

Walter Roberson
Walter Roberson 2016-7-18
If you are using R2014b or later you can use gobjects to allocate placeholder objects. You can then use isa() to test the type of any one object. It looks to me as if it might be cleaner for you to also keep a logical vector of which entries are active or not.
  3 个评论
Walter Roberson
Walter Roberson 2016-7-18
Have you considered instantiating all of the rectangles as valid, but setting the ones you do not need to 'Visible', 'off', and then manipulate the properties of the rectangles at need? For example, a "new" rectangle is just setting the Position (and curvature?) of an unused rectangle and then setting it Visible

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by