How do I add new imroi objects to an array?

2 visualizzazioni (ultimi 30 giorni)
At the moment, I am creating new imroi objects manually like so:
line1 = imline(main_axes, [1 2], [1 2], ...
'PositionConstraintFcn', constraint_line);
line2 = imline(main_axes, [2 3], [2 3], ...
'PositionConstraintFcn', constraint_line);
line3 = imline(main_axes, [3 4], [3 4], ...
'PositionConstraintFcn', constraint_line);
However, what I really want is to store the imroi objects in an array. Something a bit like this:
line_objects(line1, line2, line3)
Then my objective is to use this to add line objects from the press of a button like so:
line_button = uicontrol('style', 'pushbutton',...
'string', 'Add a line', ...
'callback', {@add_line, line_objects});
Can anyone give some general advice on how to approach this?

Risposta accettata

Rebecca Krosnick
Rebecca Krosnick il 21 Dic 2015
My understanding is that you would like to use the "line_objects" variable across callbacks in your GUI. Rather than trying to pass a "line_objects" variable around as an argument to "add_line", I suggest sharing the variable using "setappdata"/"getappdata" or "guidata". The following link provides an overview of different approaches for sharing data in GUIs as well as examples: http://www.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html
  1 Commento
Hasan Aslam
Hasan Aslam il 21 Dic 2015
Modificato: Hasan Aslam il 21 Dic 2015
Hey, thanks for the reply. I actually posted this a while ago now and I managed to think of a way using the button UserData attribute (which I think is similar to the guidata you mentioned). Here is my solution in case anybody wishes to use it in the future.
I used a cell array to store created object handles, then I stored this array in the 'UserData' attribute of the button. This is all done inside the button callback:
if isempty(get(object_handle, 'UserData'))
% If the array doesn't exist, create it
object_array = cell(10, 1);
set(object_handle, 'UserData', imroi_array);
end
% Retrieve the array from the 'UserData' property
object_array = get(object_handle, 'UserData');
Then I found this handy function online to find the next empty cell (Source: http://stackoverflow.com/questions/3400515/how-do-i-detect-empty-cells-in-a-cell-array)
% Find the next empty cell to store our imroi object
next_cell = find(cellfun(@isempty, object_array),1);
object_array{next_cell} = imline(gca, [3 4], [3 4], ...
'PositionConstraintFcn', constraint);
And finally, update the user_data again:
% Update the UserData attribute
set(object_handle, 'UserData', imroi_array);
Oh, and it doesn't handle deleted objects yet. You can easily do so by checking at the start of the callback if any of the objects have been deleted using isvalid(handle), and set the cell array index to {}.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by