Changing class properties with callback function

14 views (last 30 days)
Sven on 7 Oct 2016
Commented: Sven on 7 Oct 2016
I want to create a control that is handling my figures. I will put down a minimal example here. My main problem is how to change a class property (m_CurFigureIndex), when executing a callback function.
So the main routine is:
close all;
FC = C_MinFigureControl;
FC = FC.ActivateEventControls;
And my class definition:
classdef C_MinFigureControl
methods (Access = public)
function obj = C_MinFigureControl
obj.m_CurFigureIndex = 1;
obj = obj.GetAllFigH;
end % function C_MinFigureControl
function obj = GetAllFigH(obj)
obj.m_FigureHandle = findobj('Type','figure');
if ~isempty(obj.m_FigureHandle)
[~, ind] = sort([obj.m_FigureHandle.Number]);
obj.m_FigureHandle = obj.m_FigureHandle(ind);
end % if not empty
end % function GetAllFigH
function obj = Scroll(obj, ScrollCount)
obj.m_CurFigureIndex = obj.m_CurFigureIndex + ScrollCount;
obj.m_CurFigureIndex = max(obj.m_CurFigureIndex,1);
obj.m_CurFigureIndex = min(obj.m_CurFigureIndex,length(obj.m_FigureHandle));
end % function Scroll
function obj = ActivateEventControls(obj)
end % function ActivateEventControls
function FigScroll(obj,src,CallBackData)
ScrollCount = 0;
if strcmp(CallBackData.EventName,'WindowScrollWheel')
ScrollCount = CallBackData.VerticalScrollCount;
end % if CallBackData
obj = obj.Scroll(ScrollCount);
So I am getting all figure handles and then create a callback function for scrolling with the mousewheel (FigScroll). I wanna change m_CurFigureIndex on each scroll to keep track of the current figure. This one does not work, I do not know how to pass back the value, so that the real class does change. Appears there is always just the copy changing. So next scroll event I am always back to m_CurFigureIndex = 1.
I would appreciate any hint for a fix or a hint what a more proper way of coding this is.
Thank you in advance Sven

Accepted Answer

Adam on 7 Oct 2016
Edited: Adam on 7 Oct 2016
Derive your class from handle is the easiest way:
classdef C_MinFigureControl < handle
This brings in a few differences from a by-value class, one of which being that the object as by-reference copy semantics.
Note that if you do that you don't need to return obj out of your functions and reassign the object. It doesn't do any harm, but is redundant for a handle-derived class.
  1 Comment
Sven on 7 Oct 2016
Thanks alot, that was exactly what I am looking for. Just changing the line as you indicated did the job. Thought it must be something small.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!