if statement asking if an addlistener() event has occured

Is there a way to write an if statement that says something like:
if % addlistener(handles.Edit1, 'String','PostGet',f) occured %do something end

 Accepted Answer

I don't really understand why you would want to do this. The purpose of the listener is to fire when something (some event) happens. Why then do you need an if()? When this something happens, the listener will fire and your if() will happen automagically.

13 Comments

Here's what I discovered would happen after I got the addlistener() to work. Let's say I use the drop down menu and select the 5th option. The 5th option doesn't use all of the parameters available. The ones that it doesn't use I used set(handles.Edit1, 'Enable', 'off'); to gray it out. This is great because out of the 22 different options, a user could see which parameters each one uses. Obviously on Manual, I want all parameters enabled. With my addlistener() in place, if I choose option 5, when I edit the value in an edit box, it will switch to manual however any edit text boxes that were disabled remain disabled. I was going to create an if statement that told the GUI to re-enable all parameters if the popup menu was switched to Manual. Does that make any sense?
Just have the same listener enable them all!
addlistener(...,@manualAndEnable)
function manualAndEnable(src,evt)
set(h1,'enable','on');
set(h2,'enable','on');
etc..
Tried messing around with this for awhile. I'm sure it's a simple fix but I'm not seeing it. I attempted to do what you had suggested and maybe you can tell me what I did wrong Sean. Here's my code:
function ManualAndEnable(~,~)
set(handles.Profile,'Value',1);
set(handles.RelativePower1, 'Enable', 'on');
set(handles.RelativePower2, 'Enable', 'on');
set(handles.RelativePower3, 'Enable', 'on');
set(handles.RelativePower4, 'Enable', 'on');
set(handles.DelayProfileShape1, 'Enable', 'on');
set(handles.DelayProfileShape2, 'Enable', 'on');
set(handles.DelayProfileShape3, 'Enable', 'on');
set(handles.DelayProfileShape4, 'Enable', 'on');
set(handles.DopplerSpread1, 'Enable', 'on');
set(handles.DopplerSpread2, 'Enable', 'on');
set(handles.DopplerSpread3, 'Enable', 'on');
set(handles.DopplerSpread4, 'Enable', 'on');
set(handles.DopplerShift1, 'Enable', 'on');
set(handles.DopplerShift2, 'Enable', 'on');
set(handles.DopplerShift3, 'Enable', 'on');
set(handles.DopplerShift4, 'Enable', 'on');
set(handles.DopplerShiftDelay1, 'Enable', 'on');
set(handles.DopplerShiftDelay2, 'Enable', 'on');
set(handles.DopplerShiftDelay3, 'Enable', 'on');
set(handles.DopplerShiftDelay4, 'Enable', 'on');
set(handles.FilterOrder1, 'Enable', 'on');
set(handles.FilterOrder2, 'Enable', 'on');
set(handles.FilterOrder3, 'Enable', 'on');
set(handles.FilterOrder4, 'Enable', 'on');
set(handles.RelativeDelay2, 'Enable', 'on');
set(handles.RelativeDelay3, 'Enable', 'on');
set(handles.RelativeDelay4, 'Enable', 'on');
set(handles.DelaySpread_us1, 'Enable', 'on');
set(handles.DelaySpread_us2, 'Enable', 'on');
set(handles.DelaySpread_us3, 'Enable', 'on');
set(handles.DelaySpread_us4, 'Enable', 'on');
set(handles.DelayPeak_us1, 'Enable', 'on')
set(handles.DelayPeak_us2, 'Enable', 'on');
set(handles.DelayPeak_us3, 'Enable', 'on');
set(handles.DelayPeak_us4, 'Enable', 'on');
set(handles.EnableButton2, 'Value', 0);
set(handles.EnableButton3, 'Value', 0);
set(handles.EnableButton4, 'Value', 0);
Getting an error for each line that all states:
Undefined variable "handles" or class "handles.Profile".
Error in channel_model_gui>ManualAndEnable (line 193)
set(handles.Profile,'Value',1);
Warning: Error occurred while evaluating listener callback.
> In openfig at 135
In gui_mainfcn>local_openfig at 286
In gui_mainfcn at 234
In channel_model_gui at 42
Undefined variable "handles" or class "handles.Profile".
The listener you add should be an anonymous function that captures the figure number of the GUI and passes it to the callback. Then in the callback, use guidata() applied to that figure to pull back the handles structure.
handles = guidata(TheFigureNumber);
So I can't have an addlistener(handles.Edit1,'String','PostGet',@ManualAndEnable);?
I figured doing that it would call this function that is intended to run all of that posted above.
addlistener(handles.Edit1,'String','PostGet',@(src, evt) ManualAndEnable(src, evt, gcf))
function ManualAndEnable(~,~, GUI_Figure_number)
handles = guidata(GUI_Figure_number)
Is this what you were saying I should be doing Walter?
addlistener(handles.Edit1,'String','PostGet',@(~,~) ManualAndEnable(handles.Edit1,'String', gcf))
function ManualAndEnable(~,~, GUI_Figure_number)
handles = guidata(GUI_Figure_number)
set(handles.Profile,'Value',1);
set(handles.Edit1, 'Enable', 'on');
This causesthe following errors:
Attempt to reference field of non-structure array.
Error in channel_model_gui>ManualAndEnable (line 104)
set(handles.Profile,'Value',1);
Error in channel_model_gui>@(~,~)ManualAndEnable(handles.RelativePower1,'String',gcf) (line
57)
addlistener(handles.RelativePower1,'String','PostGet',@(~,~)ManualAndEnable(handles.RelativePower1,'String',gcf));
Warning: Error occurred while evaluating listener callback.
Please put a breakpoint in at the "handles = " and see what value you are getting for GUI_Figure_number, and then what result you get from the guidata() call.
GUI_Figure_number value is 1, and right after the guidata() call I get a Figure 1 display that is all gray. handles is showing a value of [ ] in my workspace.
Skip the whole GUIDATA thing. Instead of passing gcf has the third input, pass handles.
,@(~,~)ManualAndEnable(handles.RelativePower1,'String',handles));
then in ManualAndEnable(src,evt,handles)
etc...
IT WORKS!! Thanks so much guys!
Your handles must have been associated with a different figure number I guess.
or improperly GUIDATAed (new verb!). That is why I avoid GUIDATA like the plague.

Sign in to comment.

More Answers (1)

Have the listener set a variable in an area you can test.

1 Comment

So could I use an expression such as
addlistener(handles.Edit1, 'String', 'PostGet', n=1);
then test my n variable? I tried it and it doesn't like the expression at all and I can't find any documentation why.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!