Find custom graphics control object from graphics handle

조회 수: 2 (최근 30일)
Darim
Darim 2017년 1월 27일
답변: Steven Lord 2017년 1월 27일
I am trying to implement a controller family, managing gui interaction. My class should replace the standard m-file created when using guide.
Currently the controller class is very simple:
classdef Figure < handle
%FIGURE Summary of this class goes here
% Detailed explanation goes here
properties
gfx
end
methods
function obj = Figure()
% create new figure
obj.gfx = matlab.ui.Figure();
end
end
end
I would now like to add other elements to my figure. Matlab graphics therefore implements a composite pattern which enables adding or removing other matlab.ui elements in a simple fashion, i.e. each element has "Parent" and "Children" references. Moreover this enables travelling up and down the tree representing the structure of the graphical user interface.
At some point I might like to find my corresponding customized controller object. But since the composite is located in matlab.ui, this is not directly accessible.
What is the best solution to achieve this?
Thanks, Daniel
  댓글 수: 3
Darim
Darim 2017년 1월 27일
Adam, thanks for the quick reply! Maybe my description is a little bit confusing, sorry. Please let me add some code.
I enhanced my controller class:
classdef Figure < handle
%FIGURE Summary of this class goes here
% Detailed explanation goes here
properties
gfx
end
methods
function obj = Figure()
% create new figure
obj.gfx = matlab.ui.Figure();
% add callback to figure
set(obj.gfx, 'ButtonDownFcn', @obj.atButtonDown);
set(obj.gfx, 'DeleteFcn', @obj.atDelete);
end
end
methods
function delete(obj)
disp('Controller deleted')
end
end
methods
function atButtonDown(obj, src, evnt)
disp('Hello')
end
function atDelete(obj, src, evnt)
delete(obj);
end
end
end
I create a new figure:
Figure();
I store the figure:
savefig('f')
I close and clear everything:
close all
clear all
I reload the figure:
openfig('f.fig')
Then I click in the figure, which executes the callback. Obviously Matlab has restored the controller attached to the callbacks. This is nice. But I'd like to acces the controller object or even all controller objects in the subtree of the matlab.ui, i.e. all Children controller (i.e. the composite in Matlab graphics object's property "Children"). I even cannot get the controller of the figure. I hope this is more clear now. Can you give a hint?
Thanks, Daniel
Adam
Adam 2017년 1월 27일
If you use savefig and openfig all you are saving and loading is the Matlab figure and its contents. The object of your 'Figure' class is not saved or reloaded at all.
To do this you would need to save it as a .mat file not saving the current figure.
I still don't understand what you define as a 'controller' though. Do you just mean the graphics object? I have various classes containing the word controller, but they contain many functions that I have written that act on figures and their components so the terminology is confusing to me in your case.
If you are referring to your Figure class as the controller then I'm not sure what you mean by 'all controller objects in the subtree' as you only have the one controller, which is your 'Figure' object.
You can access child objects via the usual method of
gfx.Children
and then onwards down the tree from there.

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2017년 1월 27일
It sounds like you may be trying to do the same type of thing App Designer is doing. You may want to see if that satisfies your needs (or comes close enough that you can use the code it creates as a starting point.)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by