Using listbox in MATLAB GUI
21 views (last 30 days)
Show older comments
Avinav Kumar
on 14 Mar 2021
Commented: Avinav Kumar
on 16 Mar 2021
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 Comments
Accepted Answer
Image Analyst
on 14 Mar 2021
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 Comments
Image Analyst
on 15 Mar 2021
Set a breakpoint and just step through until you find the cause of the error.
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps 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!