Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.
|
|
Matthew
Posts:
63
Registered:
6/7/10
|
|
Re: automatically update editbox of one GUI with contents entered in 2nd GUI
Posted:
Apr 11, 2012 11:04 PM
|
|
Sure, This is the documentation you want to look at
http://www.mathworks.com/help/techdoc/matlab_oop/brb6gnc.html
The basic idea is that you create a handle object with properties with 'SetObservable' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% object: classdef test_obj < handle properties (SetObservable, AbortSet) a; b; c; end
events overflow; end
methods function set.a(this,value) if value > 10 notify(this,'overflow'); fprintf('a cannot exceed 10'); else this.a = value; end end end
end
%%%%%%%%%%%%%%%%%%%%%
now create an instance of the object:
%%%%%
test = test_obj;
%%%%%%
Now you can register listeners
%%%%%%%%%%%%%%%%% % you can listen to an event, which you throw manually in the code: %Whenever the overflow event is thrown, the callback is executed. The callback is %always fed two parameters, the object sending the notification and event data
list{1} = test.addlistener('overflow',@(obj,event)fprintf('OVERFLOW\n'));
% you can also listen to individual properties in the same way. When the property % changes, the callback is called. You can call use "PreSet", "PostSet", "PreGet' and % "PostGet" to decide when the callback is triggered.
list{2} = t.addlistener('b','PostSet',@(obj,event)fprintf('B was just set\n'));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
In a gui, I register my listeners like this:
handles.listeners{1} = handles.object.addlistener('property','PostSet',@(obj,event)updatedisplay(handles));
This causes the updatedisplay callback (in the gui) to be called every time object.property is updated.
Then in your GUI, all you have to do is directly update the object and let the listeners do their thing. Since the object is a handle class, you can update any "copy" of it as all copies are just references to the same object. This lets you manipulate the object (and thus any attached GUIs) from a script or command line as well, and the GUI's will auto update to reflect the changes.
Just make sure you include a close function in your GUI that deletes the listeners or you will wind up with some odd "object deleted" errors if the listeners become orphaned.
Hope this helps.
"Alex " <blue.harvest.83@gmail.com> wrote in message <jm5dgp$fg1$1@newscl01ah.mathworks.com>... > Thanks Matthew, I think your suggestion about listeners is what I am looking for, however I am not familiar with listeners, and "help listener" doesn't return any results . Could you give me a brief example of code that would utilize listeners with respect to my problem? > Thanks > > "Matthew" wrote in message <jm5cit$cdg$1@newscl01ah.mathworks.com>... > > you can use guidata: > > http://www.mathworks.com/help/techdoc/creating_guis/f13-998449.html > > > > However, the way I've approached this problem in the past is to write all of my processing code into a handle object. The GUI takes the handle to the object and registers listeners on any properties and events that it cares about with proper callbacks to deal with the change in data. So instead of having to write one GUI to talk to the other, you just write each GUI to update the base object and update itself when something it cares about in the base object changes.
|
|
|
|