Changing class properties with callback function
9 次查看(过去 30 天)
显示 更早的评论
Hi,
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;
figure('name','F1')
figure('name','F2')
figure('name','F3')
FC = C_MinFigureControl;
FC = FC.ActivateEventControls;
And my class definition:
classdef C_MinFigureControl
properties
m_FigureHandle
m_CurFigureIndex
end
methods (Access = public)
function obj = C_MinFigureControl
obj.m_CurFigureIndex = 1;
obj = obj.GetAllFigH;
figure(obj.m_FigureHandle(obj.m_CurFigureIndex))
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));
figure(obj.m_FigureHandle(obj.m_CurFigureIndex))
end % function Scroll
function obj = ActivateEventControls(obj)
set(obj.m_FigureHandle,'WindowScrollWheelFcn',@obj.FigScroll);
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);
end
end
end
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
0 个评论
采纳的回答
Adam
2016-10-7
编辑:Adam
2016-10-7
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!