Changing a popup menu by changing an edit text box
2 次查看(过去 30 天)
显示 更早的评论
Adam Kaas
2012-5-18
Sorry for all the questions today guys, but I really appreciate all of your help. So I've learned how to change an edit text box by changing a popup menu but can't seem to figure out how to do the opposite. Is it possible that if someone changes the value of an edit text box, that my popup menu can change automatically? I would assume it'd be some sort of if statement that checks if the values are the same but that seems terribly inefficient so I am guessing I am wrong. Thanks again!
采纳的回答
Sean de Wolski
2012-5-18
The edit uicontrol's callback function should get it's string and set something else in the popup.
More per comments
In this case you should use addlistener() to add a listener to the 'PostSet' 'string' event broadcasted by each edit box. The documentation for addlistener() is here:
The call would look something like this:
for ii = 1:numel(hEdit) %where hEdit is the vector of edit box handles
addlistener(hEdit(ii),'PostSet','string',@(src,evt)set(hPopUp,'value',1))
end
17 个评论
Adam Kaas
2012-5-18
That makes sense to me. What I'm confused on is I have 23 different options on my drop down menu. The first one is 'Manual' and the rest have preset values for my edit boxes. What I want is when any one edit box value is changed, I want the popup menu to switch to 'Manual.' So one of my callback functions (let's say edit1_callback) I would be looking to do something like this:
manualcheck = get(handles.edit1, 'String');
if manualcheck ~= get(handles.edit1, 'String');
set(handles.popup, 'String', 'Manual');
end
I'm not sure why I can't see why it doesn't work. I know that code I wrote seems way too simple.
Adam Kaas
2012-5-18
Okay, I see why it doesn't work, as manualcheck will always equal get(handles.edit1, 'String'). I'm still unsure how to fix it though.
Walter Roberson
2012-5-18
Use strcmp() to compare strings.
You are comparing get(handles.edit1, 'String') to get(handles.edit1, 'String') so of course it will always compare the same.
You might as well just go ahead and always do
set(handles.popup, 'String', 'Manual')
unless what you are trying to do is to _not_ change to Manual unless there was an actual change to the edit box (not just activating the callback by pressing return without any change.)
Adam Kaas
2012-5-18
Thank you very much. I'll see if I can figure this out. Struggling understanding the @(src,evt) part of that code, but the documentation should hopefully point me in the right direction.
Sean de Wolski
2012-5-18
most call backs are automatically passed src, evt, the src is the handle to the object calling the callback and evt is event data.
Walter Roberson
2012-5-18
Side note: the only "callbacks" that I have found that are _not_ passed src and evt, are "filter functions" such as functions that constrain where a point can be dragged to. Those unusual functions return a value, whereas regular callback functions cannot return a value.
Adam Kaas
2012-5-18
so for my src, would it be my handles.Edit1 or handles.popup? I would think it'd be my Edit1.
and the event data is something I feel I just am not grasping. Is it going to be something like PostSet again or is it going to be looking for an if statement that's looking for a change or am I not even close?
I've always liked MATLAB and never really did anything with GUI's before this week, so I apologize for my terrible learning curve on this.
Sean de Wolski
2012-5-18
What is passed in his handles.Edit1, because that's the dohickey being listened to. I am not using that it the creation of my callback because, you don't care which edit box called, you want the popup to be set to manual (i.e. no use for src, evt - we just have to account for them). You could deny them as inputs with tildes instead:
@(~,~)set(...)
We know the handle to the popup a priori and so it is seen in the creation of the function handle and will be updated when that callback is fired.
Adam Kaas
2012-5-18
Well denying them certainly makes that easier.
This is the code I'm using:
function Edit1_Callback(hObject, eventdata, handles)
for ii = 1:numel(handles.Edit1)
addlistener(handles.Edit1(ii),'PostSet','String',@(~,~)set(handles.Popup,'value',1))
end
This is the error I am getting:
Error using NAMEofGUI>Edit1_Callback (line 1090)
callback types have to be one of {PostSet,PostGet,PreGet,PreSet} and match case
So my questions are these:
1. Am I putting this code in the correct location?
2. Am I abusing my posting privilages? I'm really trying to not have you write this for me and understand it myself so I can use it again because I think this is a great function.
Sean de Wolski
2012-5-18
My bad in the original example:
addlistener(h,'String','PostSet',f), I swapped arguments (note to self: always read the doc Sean!)
I will point out another bug too. You are looping through handles.edit1. If handles.edit1 is just the handle to one edit box, then this will only add a listener to _that_ edit box. Since you have more edit boxes, you will have to add tthe listeners to those i.e. handles.edit2 handles.edit3 (not handles.edit1(3)). Before I was assuming you could have a vector of handles. If this GUI is written completely programmatically, you could indeed have this. If it;s in GUIDE, skip the for loop, copy and paste:
f = @(~,~)set(handles.Popup,'value',1)
addlistener(handles.Edit1,'PostSet','String',f)
addlistener(handles.Edit2,'PostSet','String',f)
etc.
And you aren't abusing your posting priviledges. The questions are to the point with enough information we can do something and it's obvious you're trying.
Adam Kaas
2012-5-21
Thank you very much for all your help with this. It's starting to make more sense to me. I was receiving some weird errors earlier and discovered my problem (forgot to delete the code from Friday!) and now the good news is I'm receiving no errors. The unfortunate bad news is that it appears that nothing is happening when I change one of my edit text boxes. What I think the problem is is my lack of understanding in terms of where to place these listener functions. I was thinking I could place them in the callback functions but I would actually get errors when doing that (essentially same error as before of invalid callback type). Where else do you suggest I try? I've tried other functions and virtually everywhere in the code, but no results anywhere so far.
Sean de Wolski
2012-5-21
Assuming that you want them to be present all of the time, you should add them in the openingfcn of the GUIDE GUI.
Sean de Wolski
2012-5-21
Here's a small example:
function listenExample
figure('units','pix');
hedit = uicontrol('style','edit','position',[10 10 100 30],'string','hello world');
hlist = uicontrol('style','listbox','position',[150 150 100 100],...
'string',{'Auto';'Manual'},'value',1);
addlistener(hedit,'String','PostSet',@(~,~)set(hlist,'value',2));
Adam Kaas
2012-5-21
I thought I should try putting them in there, so I did before and received this:
Error using channel_model_gui>channel_model_gui_OpeningFcn (line 62)
callback types have to be one of {PostSet,PostGet,PreGet,PreSet} and match case
Error in gui_mainfcn (line 221)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in channel_model_gui (line 42)
gui_mainfcn(gui_State, varargin{:});
My GUI won't even open when I place it there. I know this is probably getting annoying as I'm getting slightly frustrated (mainly because I'm so confused as to why it doesn't work). I'm going to copy and paste my actual code that I typed up in my openingfcn:
f = @(~,~)set(handles.Profile,'Value',1);
addlistener(handles.RelativePower1,'PostSet','String',f)
addlistener(handles.RelativePower2,'PostSet','String',f)
addlistener(handles.RelativePower3,'PostSet','String',f)
addlistener(handles.RelativePower4,'PostSet','String',f)
addlistener(handles.DelayProfileShape1,'PostSet','String',f)
addlistener(handles.DelayProfileShape2,'PostSet','String',f)
addlistener(handles.DelayProfileShape3,'PostSet','String',f)
addlistener(handles.DelayProfileShape4,'PostSet','String',f)
addlistener(handles.DopplerSpread1,'PostSet','String',f)
addlistener(handles.DopplerSpread2,'PostSet','String',f)
addlistener(handles.DopplerSpread3,'PostSet','String',f)
addlistener(handles.DopplerSpread4,'PostSet','String',f)
addlistener(handles.DopplerShift1,'PostSet','String',f)
addlistener(handles.DopplerShift2,'PostSet','String',f)
addlistener(handles.DopplerShift3,'PostSet','String',f)
addlistener(handles.DopplerShift4,'PostSet','String',f)
addlistener(handles.DopplerShiftDelay1,'PostSet','String',f)
addlistener(handles.DopplerShiftDelay2,'PostSet','String',f)
addlistener(handles.DopplerShiftDelay3,'PostSet','String',f)
addlistener(handles.DopplerShiftDelay4,'PostSet','String',f)
addlistener(handles.FilterOrder1,'PostSet','String',f)
addlistener(handles.FilterOrder2,'PostSet','String',f)
addlistener(handles.FilterOrder3,'PostSet','String',f)
addlistener(handles.FilterOrder4,'PostSet','String',f)
addlistener(handles.RelativeDelay2,'PostSet','String',f)
addlistener(handles.RelativeDelay3,'PostSet','String',f)
addlistener(handles.RelativeDelay4,'PostSet','String',f)
addlistener(handles.DelaySpread_us1,'PostSet','String',f)
addlistener(handles.DelaySpread_us2,'PostSet','String',f)
addlistener(handles.DelaySpread_us3,'PostSet','String',f)
addlistener(handles.DelaySpread_us4,'PostSet','String',f)
addlistener(handles.DelayPeak_us1,'PostSet','String',f)
addlistener(handles.DelayPeak_us2,'PostSet','String',f)
addlistener(handles.DelayPeak_us3,'PostSet','String',f)
addlistener(handles.DelayPeak_us4,'PostSet','String',f)
Do you see any errors?
Sean de Wolski
2012-5-21
Yes. Switch the 'String' and the 'PostSet' in your calls to ADDLISTENER. (doc addlistener to see the correct syntax)
Adam Kaas
2012-5-21
Thank you Sean! Still a couple bugs to work out but I should be good to go from here!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)