How to track second figure like the Property Inspector window?

5 visualizzazioni (ultimi 30 giorni)
I want to make my own GUI that has a main figure window and a second that acts as the control panel, and "sticks"to the main figure much like the Property Inspector window does for a figure. I tried to create a listener for the position property change
f = figure;
l = addlistener(f, 'Position', 'PostSet', @(src,evt) disp(src))
which throws an error:
Error using matlab.ui.Figure/addlistener
While adding a PostSet listener, property 'Position' in class
'matlab.ui.Figure' is not defined to be SetObservable.
I tried this with the OuterPosition, InnerPosition, etc. with the same result.
I then tried to see what I could do with some of the callbacks available on the figure, but they seem to only work when inside the figure, not on the title bar where you would grab it to move it.
I know I could use SizeChangeFcn if it gets resized, but how can I move it to always be say 10 pixels left of the figure?
I even tried to subclass matlab.ui.Figure to change its position property to SetObservable but it is sealed.
  1 Commento
Chris Dean
Chris Dean il 15 Feb 2020
It is gross but seems to be the only way to do it that I know of. Maybe there's something buried in Java layer?
classdef FigureWithHelper < handle
properties
hFigMain;
hFigOptions;
tmr;
end
methods
function obj = FigureWithHelper()
obj.generateMainFigure();
obj.generateHelperFigure();
end
function generateMainFigure(obj)
obj.hFigMain = figure();
end
function generateHelperFigure(obj)
REPOS_TIMEOUT = 0.5;
obj.hFigOptions = figure();
obj.setHelperPos();
obj.tmr = timer('ExecutionMode', 'fixedRate',...
'Period', REPOS_TIMEOUT, ...
'TimerFcn', @obj.setHelperPos,...
'StopFcn', @(src, evt) delete(src));
start(obj.tmr);
end
function setHelperPos(obj, src, evt)
windowWidth = 250;
windowSpacer = 10;
p = obj.hFigMain.Position;
p(1) = p(1) - windowWidth - windowSpacer;
p(3) = windowWidth;
obj.hFigOptions.Position = p;
end
end
end

Accedi per commentare.

Risposte (1)

Zachary
Zachary il 14 Mar 2023
It is unfortunate that the Position property of the Figure class is not SetObservable.
However – at least as far back as R2016b (didn't check any further) – MathWorks defined several useful Events broadcast by the Figure class. Two of these events are LocationChanged and SizeChanged. You can listen for these events instead.
For anyone that stumbles upon this post and wants to create a MATLAB App that follows a figure window like Property Inspector, attached is a template.
With the MLAPP-file somewhere on your Search Path, this code creates a figure and attaches the "following" app.
leader = figure('Name',"Leader");
Follower(leader)

Categorie

Scopri di più su App Building in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by