Add/Remove List (GUI)

2 views (last 30 days)
Ana Gonçalves
Ana Gonçalves on 4 Jun 2020
Commented: Rik on 4 Jun 2020
Hi Everybody! I am here to answer a question that I had and at that time I didn't found a solution. Now that I have a solution, I would like to share with the comunity :)
It is a classical add/remove list where you transfer items from list 1 to list 2. In my code the first list (populated one) is called listbox6, and the second one is called listbox7.
It is possible to add multiple items and remove the same added items (only the group, not individual remove in this case). In other words, all that you added together can be removed together.
>To multiselection list go in the guide property inspector (or double click the list in the guide editor), and change property 'MAX' to 2.0 instead of 1.0.
Following is the code which allows you to add/remove one item in the second list.
[previous code that do not matters here...]
%listbox6
strlist6=strcat(strModelFolder,'\',str2,'\Trial');
Infolder = dir(strlist6);
MyListOfFiles = {Infolder(~[Infolder.isdir]).name};
set(handles.listbox6,'String', MyListOfFiles);
% --- Executes on selection change in listbox6.
function listbox6_Callback(hObject, eventdata, handles)
contents = cellstr(get(hObject,'String'));
List6=contents{get(hObject,'Value')};
%--- Executes during object creation, after setting all properties.
function listbox6_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function pushbutton3_CreateFcn(hObject, eventdata, handles) %Add Pushbutton
% hObject handle to editAdd (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
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to editAdd (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a = get(handles.listbox6,'string');
temp = char(a);
b = get(handles.listbox6,'Value');
choice1 = a(b); %take 'b' element in 'a' list
% c1 = strvcat(c,b)
c = get(handles.listbox6,'Value');
set(handles.listbox7,'string',choice1)
% --- Executes during object creation, after setting all properties.
function pushbutton4_CreateFcn(hObject, eventdata, handles) % Remove pushbutton
% hObject handle to editRemove (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
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to editRemove (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 editRemove as text
% str2double(get(hObject,'String')) returns contents of editRemove as a double
a = get(handles.listbox7,'string');
c= get(handles.listbox7,'Value');
choice = a(c);
choice(c,:) = [];
set(handles.listbox7,'string',choice)
  1 Comment
Rik
Rik on 4 Jun 2020
Try to make a MWE. This post still contains a lot of code that is not relevant for the solution.
You should edit this question to be an actual question and post your solution as an answer. Then other people can contribute their solutions as well.
Also, you don't need to use GUIDE to set the Max property (and I would suggest setting it to a value that is higher than 2, preferably inf or the number of elements in your list). If you insist on GUIDE you can also set this property when your GUI starts. For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.

Sign in to comment.

Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!