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

Ankit
Ankit 2019 年 9 月 18 日
what do you mean by result to be added in the respective listbox?
You mean transfer selected options (more than 1) to another listbox?
ANURAG DEEPAK
ANURAG DEEPAK 2019 年 9 月 18 日
I have a listbox in which a user can select multiple choices and a popupmenu with their corresponding choices. In the edit text box i want the multiplied result of selected values(which is to added as the user can select more than one items) of listbox and poupmenu.
function listbox1_Callback(hObject, eventdata, handles)
contents= cellstr(get(hObject,'String'));
lischoice=contents(get(hObject,'Value'));
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
set(handles.listbox1,'Userdata',l_value);
assignin('base','l_value',l_value);
function popupmenu1_Callback(hObject, eventdata, handles)
contents=cellstr(get(hObject,'String'));
popchoice1=contents(get(hObject,'Value'));
if(strcmp(popchoice1,'x'))
pop1val=10;
elseif(strcmp(popchoice1,'y'))
pop1val=20;
elseif(strcmp(popchoice1,'z'))
pop1val=30;
end
set(handles.popupmenu1,'UserData',pop1val);
assignin('base','pop1val',pop1val);
function pushbutton1_Callback(hObject, eventdata, handles)
pop1val=evalin('base','pop1val');
l_value=evalin('base','l_value');
c=pop1val*l_value;
set(handles.edit1,'String',c);
Ankit
Ankit 2019 年 9 月 19 日
your listbox1_Callback returns single value according to your code.
ANURAG DEEPAK
ANURAG DEEPAK 2019 年 9 月 19 日
Sir my question remain same as how can i obtain multiple selection in listbox and add them up. For example ,if the user selects "a" and "c" choices in listbox then the result shown by listbox callback should be 0.40 which is to be shown in "base" workspace???
Ankit
Ankit 2019 年 9 月 19 日
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 日

0 投票

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 件)

カテゴリ

ヘルプ センター および File Exchange で App Building についてさらに検索

製品

リリース

R2019a

タグ

質問済み:

2019 年 9 月 17 日

コメント済み:

2019 年 9 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by