Using listbox in MATLAB GUI
21 ビュー (過去 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 件のコメント
採用された回答
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
2021 年 3 月 15 日
Set a breakpoint and just step through until you find the cause of the error.
その他の回答 (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!