GUI callbacks

4 views (last 30 days)
Sam
Sam on 15 Aug 2011
Hi,
I am trying to retrieve the values that will be in the user input boxes and also that of the list box, my code so far is:
function BevelMenu()
figBevel = figure('Color',[0.925 0.914 0.847],... % This just changes the colour of the background in the window
'Visible','off',... %Makes the window invisible
'Resize','on', ... %Allows to resize the window
'Number','off', ... %Gets rid of number in heading
'unit','normalized', ...
'Name','Positioning of a Bevel Gear', ...
'Units', 'pixels');
movegui(figBevel,'center') %Centres it
set(figBevel,'Visible','on') %Makes visible after being centered
% image = uicontrol(figBevel,...
% 'Visible','on','01.bmp')
% To display Shaft 1 Frame:
shaft_text = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.3 0.88 0.2 0.045], ...
'string','Shaft 1 Frame: ');
% List for Shaft 1
shaft1_list = uicontrol(figBevel, ...
'Style','listbox', ...
'Units','normalized', ...
'BackgroundColor',[1 1 1], ...
'String',{'a' 'b' 'c'},...
'tag','shaft1_value', ...
'Callback',{@listBoxCallback}, ...
'Value',1,'Position', [0.5 0.85 0.1 0.1]);
set(shaft1_list, 'listboxtop',1);
abs_text = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.23 0.75 0.35 0.05], ...
'string','C1 abscissa on shaft 1 frame: ');
abs_input = uicontrol(figBevel, ...
'style','edit', ...
'unit','normalized', ...
'BackgroundColor',[1 1 1], ...
'tag','abs_value', ...
'position',[0.57 0.76 0.05 0.05]);
abs_mm = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.64 0.75 0.04 0.05], ...
'string','mm');
pitch_text = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.125 0.65 0.22 0.05], ...
'string','Pitch diameter dP1: ');
pitch_input = uicontrol(figBevel, ...
'style','edit', ...
'unit','normalized', ...
'BackgroundColor',[1 1 1], ...
'tag','pitch_value', ...
'position',[0.36 0.66 0.05 0.05]);
pitch_mm = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.42 0.65 0.33 0.05], ...
'string','mm OR Mean diameter, dm1: ');
mean_input = uicontrol(figBevel, ...
'style','edit', ...
'unit','normalized', ...
'BackgroundColor',[1 1 1], ...
'tag','mean_value', ...
'position',[0.76 0.66 0.05 0.05]);
mean_mm = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.83 0.65 0.04 0.05], ...
'string','mm');
tooth_text = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.16 0.55 0.5 0.05], ...
'string','Tooth width, b: ');
tooth_input = uicontrol(figBevel, ...
'style','edit', ...
'unit','normalized', ...
'BackgroundColor',[1 1 1], ...
'tag','tooth_value', ...
'position',[0.5 0.56 0.05 0.05]);
tooth_mm = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.57 0.55 0.04 0.05], ...
'string','mm');
halftop1_text = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.2 0.45 0.2 0.08], ...
'string','Half top angle, delta1(degrees):');
halftop1_input = uicontrol(figBevel, ...
'style','edit', ...
'unit','normalized', ...
'BackgroundColor',[1 1 1], ...
'tag','halftop1_value', ...
'position',[0.43 0.46 0.05 0.05]);
halftop2_text = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.5 0.45 0.2 0.08], ...
'string','Half top angle, d2(degrees):');
%set(halftop2_text,'FontName','Symbol')
halftop2_input = uicontrol(figBevel, ...
'style','edit', ...
'unit','normalized', ...
'BackgroundColor',[1 1 1], ...
'tag','halftop2_value', ...
'position',[0.72 0.46 0.05 0.05]);
teeth_text = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.26 0.35 0.15 0.05], ...
'string','No. of teeth: ');
wheel1_text = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.41 0.35 0.15 0.05], ...
'string','First Wheel: ');
wheel1_input = uicontrol(figBevel, ...
'style','edit', ...
'unit','normalized', ...
'BackgroundColor',[1 1 1], ...
'tag','wheel1_value', ...
'position',[0.62 0.36 0.05 0.05]);
wheel2_text = uicontrol(figBevel, ...
'style','text', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'ForegroundColor','blue', ...
'FontSize',10, ...
'position',[0.4 0.27 0.2 0.05], ...
'string','Second Wheel: ');
wheel2_input = uicontrol(figBevel, ...
'style','edit', ...
'unit','normalized', ...
'BackgroundColor',[1 1 1], ...
'tag','wheel2_value', ...
'position',[0.62 0.28 0.05 0.05]);
pos_button = uicontrol(figBevel, ...
'style','push', ...
'unit','normalized', ...
'BackgroundColor',[0.925 0.914 0.847], ...
'string','Position!', ...
'Callback',{@pos_app,shaft1_list}, ...
'pos',[0.45 0.10 0.1 0.1]);
function pos_app(hObject, event) %need to get the values from all the input boxes and the value of the listbox
get(abs_input,'Tag','abs_value')
get(pitch_input,'Tag','pitch_value')
get(mean_input,'Tag','mean_value')
get(tooth_input,'Tag','tooth_value')
get(halftop1_input,'Tag','halftop1_value')
get(halftop2_input,'Tag','halftop2_value')
get(wheel1_input,'Tag','wheel1_value')
get(wheel2_input,'Tag','wheel2_value')
switch get(listbox,'Value')
case Value == 1
close(BevelMenu) %At the moment I just want to get the thing to work, so to close if good enough!
case Value == 2
close(BevelMenu)
case Value == 3
close(BevelMenu)
end
end
listBoxCallback(listBox,[]) %To initialize listbox
end
However, I am getting confused how to link all this to the push button and cannot seem to get the listbox working when the user clicks on an option. Could you please help me! =)
Thank you!

Accepted Answer

Jan
Jan on 15 Aug 2011
Please take the time to describe the actual problem with any details. It is hard to find an answer, if the problem is, that you are "confused".
There is a syntax problem in your SWITCH expression:
switch get(listbox,'Value')
case Value == 1 % NOT INTENTED BEHAVIOUR!
...
"Value==1" is LOGICAL(1), if Vlaue is 1 and LOGICAL(0) otherwise. Now the comparison with "get(listbox, 'Value')" does most likely not, what you expect. Better:
switch get(listbox,'Value')
case 1
...
case 2
... etc
[EDITED]: There are a lot of further bugs in your code. E.g.:
1. you define the callback as "'Callback',{@pos_app,shaft1_list}". Then the callback function must defined as:
function pos_app(ObjH, EventData, shaft1_list)
such that your "function pos_app(hObject, event)" will fail with an error message.
2. "get(abs_input,'Tag','abs_value')" is no valid MATLAB syntax. GET get the value of a single property. See "help get".
3. In "get(listbox, 'Value')" the variable "listbox" is not defined.
4. In "listBoxCallback(listBox,[])" the variable "listBox" is not defined also. In addition you do not post the function listBoxCallback.
I suggest to read the Getting Started sections in the documentation and study Matt's marvelous FEX: GUI examples.
  1 Comment
Sam
Sam on 15 Aug 2011
Ok thank you. That will allow me to choose an option from the listbox. How do I then connect that value to produce (for example I might put a program in to calculate an answer using all of the user inputs) the answer when the push button is pressed and not just the value in the listbox?

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!