MATLAB Answers

I am using Matlab Guide and need advice on how to multiselect from a listbox populated with numeric data and save the selected items (>2) into an array.

2 ビュー (過去 30 日間)
Mark Reed
Mark Reed 2019 年 7 月 31 日
回答済み: Jyotsna Talluri 2019 年 8 月 6 日
I have a GUI I created with Matlab GUIDE. I inserted a listbox into the GUIDE GUI and have populated it with azimuth data (0 to 360, inc 1). I am having trouble multiselecting data from it. What I would like to do is select several values with the mouse while holding down CTRL then save these values to an array. The uicontrol function will not work in GUIDE since using it give an error. My code so far:
function listbox1_az_sel_Callback(hObject, eventdata, handles)
% hObject handle to listbox1_az_sel (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
lbaz1 = guidata(hObject)
set(lbaz1.listbox1_sel_ok, 'Value', 0)
set(lbaz1.listbox1_az_sel, 'Max', length(lbaz1.azimuth))
set(lbaz1.listbox1_az_sel, 'Min', 1)
lbaz1.vals = cellstr(get(lbaz1.listbox1_az_sel, 'String'))
lbaz1.values = lbaz1.vals{get(lbaz1.listbox1_az_sel, 'Value')}
guidata(hObject, lbaz1)

  0 件のコメント


回答 (1 件)

Jyotsna Talluri
Jyotsna Talluri 2019 年 8 月 6 日
First populate your list box with the azimuth data in the opening function
function listmultbox_OpeningFcn(hObject, eventdata, handles, varargin)% file name
handles.output = hObject
for i=1:361
set(handles.listbox1,'max',length(azimuth));% for multiple selections
% Update handles structure
guidata(hObject, handles);
For each selection of data callback() function is called and all the selected items can be stored
function listbox1_Callback(hObject, eventdata, handles)
list=get(hObject,'String'); % all listbox items
persistent Items
if numel(Items) < numel(hObject.Value)
index_selected=get(hObject,'Value'); % get indices of selected items
Items = union(Items,char(list{index_selected}),'stable'); %cell elements converted to char
elseif numel(Items) > numel(hObject.Value)
Items = intersect(Items,char(list{index_selected}),'stable');
elseif numel(Items) == numel(hObject.Value) && numel(hObject.Value)==1
Items =char(list{index_selected});
disp(['Currently selected items (in order): ' cellstr(Items(:)')]);
It displays the list of selected items for each new selection
Refer to below link for more properties on listbox

  0 件のコメント



Community Treasure Hunt

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

Start Hunting!

Translated by