MATLAB Answers

Mark Reed
0

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.

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 件のコメント

サインイン to comment.

タグ

1 件の回答

回答者: Jyotsna Talluri 2019 年 8 月 6 日

Hi,
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
azimuth{i}=i-1;
end
set(handles.listbox1,'string',azimuth);
set(handles.listbox1,'max',length(azimuth));% for multiple selections
set(handles.listbox1,'min',1);
% 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)
index_selected=get(hObject,'Value');
Items = intersect(Items,char(list{index_selected}),'stable');
elseif numel(Items) == numel(hObject.Value) && numel(hObject.Value)==1
index_selected=get(hObject,'Value');
Items =char(list{index_selected});
end
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 件のコメント

サインイン to comment.



Translated by