Using an array of graphics handles as a class property

4 visualizzazioni (ultimi 30 giorni)
Hi Guys,
I'm creating a class which represents a simple GUI widget. I want to use a property to store handles for the various uicontrols.
All of the items in the array are of the same type (uicontrol), but I'm finding that Matlab keeps converting the handles to double - which breaks dot notation.
As simple example is below:
classdef widget < matlab.mixin.SetGet
% Minimal example to show my problem. A GUI with two buttons, when you
% press one button it changes the colour of the other.
properties
% I'd like to keep track of the GUI objects in an array. The array
% will only hold handles for uicontrol objects (i.e. they're all the
% same type of object)
HandleArray
end
methods
function self = widget()
% Add two buttons to the figure
self.HandleArray(1) = uicontrol('Units', 'Normalized', ...
'Position', [0, 0, 0.5 1], ...
'String', 'Left', ...
'Callback', @(s,e) self.button_pressed(s,e));
self.HandleArray(2) = uicontrol('Units', 'Normalized', ...
'Position', [0.5, 0, 0.5 1], ...
'String', 'Right', ...
'Callback', @(s,e) self.button_pressed(s,e));
end
function button_pressed(self, s, ~)
% First choose a random colour:
rgb = rand([1,3]);
if strcmp(s.String, 'Left')
% The left button has been pressed - use the HandleArray to
% change the colour of the right button
self.HandleArray(2).BackgroundColor = rgb;
else
% The RIGHT button has been pressed - use the HandleArray to
% change the colour of the LEFT button
self.HandleArray(1).BackgroundColor = rgb;
end
end
end
end
If I create an instance of this object the HandleArray property contains two doubles. Also, clicking on either button throws a dot notation error.
How can I stop Matlab from converting the handles into doubles?
B

Risposte (1)

stefano fiorentini
stefano fiorentini il 18 Giu 2020
Hi Bob,
I faced the same problem today with MATLAB 2020a. In case you haven't figured out a solution yet, one workaround is to assign the handles to two temporary variables
obj_1 = uicontrol();
obj_2 = uicontrol();
and then assign them to
self.HandleArray = [obj_1, obj_2];
Cheers,
S

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by