GUI handles in classes problem

6 views (last 30 days)
Ruben
Ruben on 11 Jun 2012
Hello,
I'm writing an OO gui program and I'm having issues trying to use the set() function to change GUI element properties. I'm simply trying to change a buttons enable property from off to on. I have attached abridged code showing the same problem below. My world is c++ programming and the way matlab handles classes etc feels rather strange so the problem might simply be caused by my misunderstanding of the system. Anyway, when I attempt to use the set() function on the handle AD.buttonExit, it works as expected when I execute the command in the initUI() function. If I try to do the same in a different function, it fails. I checked the stacks by simply printing the contens of AD. In the initUI function it clearly shows a handle value for buttonExit, it doesn't in the constructor function (or any other class-member-function). I feel like I'm making a elementary mistake, however I don't see it and I hope someone might be able to assist me.
Kind regards, Ruben Higler
code (abridged for clarity):
classdef test
properties
AppUI;
buttonExit;
end
methods
function AD = test()
%draws UI
AD.initUI();
set(AD.buttonExit, 'Enable', 'on')
AD.test2()
end
function initUI(AD)
AD.AppUI = figure('Visible','off','Position',[520,321,695,482], 'MenuBar', 'none', 'Name', '3D Particle Tracking',...
'NumberTitle', 'off', 'Resize', 'off', 'Color', [0.94,0.94,0.94]);
AD.buttonExit = uicontrol('Enable', 'off', 'Style', 'pushbutton', 'Visible', 'on', 'Position', [35,29,181,31], 'String', 'Exit');
set(AD.AppUI, 'Visible', 'on');
%1 set(AD.buttonExit, 'Enable', 'on')
end
function test2(AD)
set(AD.buttonExit, 'Enable', 'on')
end
end
end

Answers (1)

per isakson
per isakson on 16 Jun 2012
In Matlab the connection between "handle" in "Handle Graphic" and handle classes in the OO-system is that both are "references" to objects, however objects of quite different kinds.
In the Matlab OO-system there are value and handle classes. The former is immutable and the latter mutable.
Your class, test, need to inherit from handle to behave the way you expect. Change the first line of the code to
classdef test < handle
  2 Comments
Jette
Jette on 10 Oct 2012
Does this mean that all GUI-classes need to be handle classes? Do you know a documentation or tutorial dealing with GUIs using OOP?

Sign in to comment.

Categories

Find more on Handle Classes in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!