How to Add selected values of Listbox in MATLAB GUI?

조회 수: 9 (최근 30일)
ANURAG DEEPAK
ANURAG DEEPAK 2019년 9월 17일
댓글: ANURAG DEEPAK 2019년 9월 20일
function listbox1_Callback(hObject, eventdata, handles)
contents= cellstr(get(hObject,'String'));
listchoice=contents(get(hObject,'Value'));
if(strcmp(listchoice,'a'))
lis1=10;
elseif(strcmp(listchoice,'b'))
lis1=20;
elseif(strcmp(listchoice,'c'))
lis1=30;
elseif(strcmp(listchoice,'d'))
lis1=40;
elseif(strcmp(listchoice,'e'))
lis1=50;
end
assignin('base','lis1',lis1);
i have created a listbox with five options with their respected values and when i select more than 1 option , I want the result to be added in the respective listbox.
  댓글 수: 6
ANURAG DEEPAK
ANURAG DEEPAK 2019년 9월 19일
function listbox1_Callback(hObject, eventdata, handles)
contents=cellstr(get(hObject,'String'));
lischoice=contents(get(hObject,'Value'));
set(handles.listbox1,'Value');
assignin('base','lischoice',lischoice);
With this above code i am able to select multiple items in listbox, but this is giving only cell array (String). So how can i provide values to choices made in listbox and add them up?? (With reference to above codes).

댓글을 달려면 로그인하십시오.

채택된 답변

Ankit
Ankit 2019년 9월 20일
This is not the best way but a working solution!
function listbox1_Callback(hObject, eventdata, handles)
contents= cellstr(get(hObject,'String'));
lischoice=contents(get(hObject,'Value'));
if numel(lischoice)>=2
choice1 = lischoice(1);
choice2 = lischoice(2);
if (strcmp(choice1,'a'))
l1=0.10;
elseif (strcmp(choice1,'b'))
l1=0.20;
elseif (strcmp(choice1,'c'))
l1=0.30;
end
if (strcmp(choice2,'a'))
l2=0.10;
elseif (strcmp(choice2,'b'))
l2=0.20;
elseif (strcmp(choice2,'c'))
l2=0.30;
end
l_value = l1 + l2;
else
if (strcmp(lischoice,'a'))
l_value = 0.10;
elseif (strcmp(lischoice,'b'))
l_value = 0.20;
elseif (strcmp(lischoice,'c'))
l_value = 0.30;
end
end
assignin('base','l_value',l_value);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by