Clear Filters
Clear Filters

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

2 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jul 2016
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 Comments
Walter Roberson
Walter Roberson on 18 Jul 2016
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

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!