Pushbutton output in Gui

I have 3 options in my GUI. I want as an output, the option number. Example pushbutton one is apple. If I click on Hexagonal, it should return '1'. The code below outputs 'hexagonal' but i want it to output '1'. Any lead in this direction, please?
My code currently:
function varargout = Gui1(varargin)
gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @Gui1_OpeningFcn, ... 'gui_OutputFcn', @Gui1_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT
handles.output = hObject;
guidata(hObject, handles);
% UIWAIT makes Gui1 wait for user response (see UIRESUME) uiwait(handles.figure1);
function varargout = Gui1_OutputFcn(hObject, eventdata, handles)
varargout{1} = hObject; varargout{2} = handles.string;
save 'guioutput' delete(hObject)
% --- Executes on button press in Hexagonal. function Hexagonal_Callback(hObject, eventdata, handles) % hObject handle to Hexagonal (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB selectedButton = get(hObject,'String') handles.string = selectedButton; n = 1; guidata(hObject, handles); uiresume(handles.figure1);
% handles structure with handles and user data (see GUIDATA)

Answers (1)

Image Analyst
Image Analyst on 8 Jun 2017
Edited: Image Analyst on 8 Jun 2017
How about
buttonNumber = menu('Pick one', 'Apple', 'Banana', 'Coconut');
Otherwise, use a radio button group, or a popup (drop down list).

4 Comments

Nidhi SRIVASTAVA
Nidhi SRIVASTAVA on 8 Jun 2017
Edited: Nidhi SRIVASTAVA on 8 Jun 2017
Hi! Thanks for the answer. Where in my GUI code can I introduce this? To repeat my question: How can I can get option number as an output when any pushbutton is clicked? Example Pushbutton one is 'hexagonal'. I need the output as '1'.
You can use a pushbutton group (like buttons on a blender). I've never used them. Generally people use radio buttons for what you want to do, or a popup. Then you just do
index = handles.popup.Value;
or look in the group callback for the radio buttons or pushbuttons.
I have a question, how to make output to static text and slider using one pushbutton?
You need to make a string for the static text, and a number for the slider. Let's say you have a number to start with. Then in the pushbutton callback you would do
handles.slider1.Value = number;
if handles.slider1.Value > handles.slider1.Max
handles.slider1.Max = handles.slider1.Value;
end
handles.text1.String = sprintf('%.3f', number);
Let's say your output is a string instead of a number. In that case, in the pushbutton callback you'd do:
handles.slider1.Value = str2double(yourString);
if handles.slider1.Value > handles.slider1.Max
handles.slider1.Max = handles.slider1.Value;
end
handles.text1.String = yourString;

Sign in to comment.

Categories

Asked:

on 8 Jun 2017

Commented:

on 21 Apr 2020

Community Treasure Hunt

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

Start Hunting!