Using listbox in MATLAB GUI

23 次查看(过去 30 天)
Hi,
I have a MATLAB GUI with listbox having numbers - 1,2, 3, 4 , 5, 6
I need to select one number say using listbox and then multiply this number with a number which users enter in textbox and feed the answer in another textbox.
How to go about this.
Thank you.
  3 个评论
Avinav Kumar
Avinav Kumar 2021-3-14
function varargout = Triallistbox(varargin)
% TRIALLISTBOX MATLAB code for Triallistbox.fig
% TRIALLISTBOX, by itself, creates a new TRIALLISTBOX or raises the existing
% singleton*.
%
% H = TRIALLISTBOX returns the handle to a new TRIALLISTBOX or the handle to
% the existing singleton*.
%
% TRIALLISTBOX('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TRIALLISTBOX.M with the given input arguments.
%
% TRIALLISTBOX('Property','Value',...) creates a new TRIALLISTBOX or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Triallistbox_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Triallistbox_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Triallistbox
% Last Modified by GUIDE v2.5 14-Mar-2021 15:59:48
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Triallistbox_OpeningFcn, ...
'gui_OutputFcn', @Triallistbox_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
% --- Executes just before Triallistbox is made visible.
function Triallistbox_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Triallistbox (see VARARGIN)
% Choose default command line output for Triallistbox
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Triallistbox wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Triallistbox_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
str1 = cellstr(get(handles.listbox1, 'string'));
val1 = get(handles.listbox1, 'value');
if isempty(val1)
selection1 = '';
else
selection1 = str1{val1};
end
a = str2double(selection1);
b = str2double(handles.edit1.String);
c = a + b;
handles.edit2.String = num2str(c);
if isnan(b)
warningMessage = sprintf('b is empty.\nPlease put something in the edit field for b.');
uiwait(errordlg(warningMessage));
handles.edit3.String = 'Please enter b';
return;
end
%a = str2double(selection1);
%b = str2double(get(handles.edit1,'String'));
%c = a + b;
%set (handles.edit2,'String',num2str(c));
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
b = str2double(handles.edit1.String);
if isnan(b)
warningMessage = sprintf('b is empty.\nPlease put something in the edit field for b.');
uiwait(errordlg(warningMessage));
handles.edit2.String = 'Please enter b';
return;
end
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
Theres an issue - When i run the program, listbox is selected, but the edit1 box contains no number. The output should display error. But it does not. However when i select other value in listbox , and then keep editbox empty, error message is dispalyed, though it should start from beginning.
Avinav Kumar
Avinav Kumar 2021-3-14
means if editbox2 contains no value from start, message should come right as i run the program no value in editbox2

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2021-3-14
Try this
% Get value from edit field 1
b = str2double(handles.edit1.String);
% Get list of everything in the listbox.
listboxItems = handles.listbox1.String;
% Get the index of the item they selected.
selectedItem = handles.listbox1.Value;
% Turn that selection into a double number.
listboxNumber = str2double(listboxItems{selectedNumber})
% Do the multiplication.
c = b * listboxNumber;
% Send the result into edit field 3.
handles.edit3.String = sprintf('%.4f', c);
Of course you should make it more robust by checking that the listbox actually has something in it, that they have actually selected one of the items, that the item is really a number, and that the edit field 2 has a valid number in it. I'm leaving all those validation checks up to you.
  7 个评论
Image Analyst
Image Analyst 2021-3-15
Set a breakpoint and just step through until you find the cause of the error.
Avinav Kumar
Avinav Kumar 2021-3-16
Thanks a lot Image Analyst.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by