1

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. 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.

-- rfhigler

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
jhonkola
  • 3,385
  • 1
  • 17
  • 32
rfhigler
  • 11
  • 2
  • Could you add a sample code where the strange behavior occurs? btw. as a first guess I would have the class inherit the `handle` class. – bdecaf Jun 11 '12 at 17:07

2 Answers2

1

Matlab classes are a bit odd: by default, they're "value" classes instead of "handle" classes. The difference is described here.

In short, making your class extend the handle class will make it behave the way you'd expect from other OO languages. Just change the class definition line to

classdef test < handle
rd11
  • 3,026
  • 3
  • 23
  • 32
0

Because you updated AD in initUI, you must output the updated variable. So, you need to change the following lines:

function initUI(AD) to function AD = initUI(AD)

and, within the test() function:

AD.initUI(); to AD = AD.initUI();

adara
  • 1,871
  • 1
  • 12
  • 6