How can i access elements of popup menu and use it in Push Button callbacks?

4 ビュー (過去 30 日間)
ANURAG DEEPAK
ANURAG DEEPAK 2019 年 9 月 16 日
コメント済み: ANURAG DEEPAK 2019 年 9 月 16 日
function popupmenu1_Callback(hObject, eventdata, handles)
contents= cellstr(get(hObject,'String'));
popchoice1 = contents(get(hObject,'Value'));
if (strcmp(popchoice1,'choice 1'))
pop1val=10;
elseif (strcmp(popchoice1,'choice 2'))
pop1val=20;
elseif(strcmp(popchoice1,'choice 3'))
pop1val=30;
end
set(handles.popupmenu1,'UserData',pop1val);
assignin('base','pop1val',pop1val);
function popupmenu2_Callback(hObject, eventdata, handles)
contents= cellstr(get(hObject,'String'));
popchoice2= contents(get(hObject,'Value'));
if (strcmp(popchoice2,'a'))
pop2val=40;
elseif(strcmp(popchoice2,'b'))
pop2val=50;
elseif(strcmp(popchoice2,'c'))
pop2val=60;
end
set(handles.popupmenu2,'UserData',pop2val);
assignin('base','pop2val',pop2val);
function pushbutton1_Callback(hObject, eventdata, handles)
pop1val=get(handles.popupmenu1,'UserData');
pop2val=get(handles.popupmenu2,'UserData');
c=pop1val*pop2val;
set(handles.edit1,'Value',str2double(c));
In the above probelm i need to calculate mutiplication of two numbers based on choices of two popup menu data and need to access that data in push button callback to calculate the mutiplication.
  1 件のコメント
Adam
Adam 2019 年 9 月 16 日
gives details of the various ways to share data between callbacks in a GUI. Personally I use the guidata approach when working with a GUIDE GUI.

サインインしてコメントする。

採用された回答

Ankit
Ankit 2019 年 9 月 16 日
Hello Anurag,
You can use either of the following solutions:
Solution.1: As you are assigning the values to the workspace you can read it by using evalin command.
function pushbutton1_Callback(hObject, eventdata, handles)
pop1val=evalin('base','pop1val');
pop2val=evalin('base','pop2val');
c=pop1val*pop2val;
set(handles.edit1,'string',c); % here you don't have to convert as the value already in double.
Solution.2:
assignin('base','pop1val',pop1val) This statement you don't require in your code. As you already setting the value of 'UserData' for popupmenu (set(handles.popupmenu1,'UserData',pop1val);)
This is how your code looks now:
function popupmenu1_Callback(hObject, eventdata, handles)
contents= cellstr(get(hObject,'String'));
popchoice1 = contents(get(hObject,'Value'));
if (strcmp(popchoice1,'choice 1'))
pop1val=10;
elseif (strcmp(popchoice1,'choice 2'))
pop1val=20;
elseif(strcmp(popchoice1,'choice 3'))
pop1val=30;
end
set(handles.popupmenu1,'UserData',pop1val);
function popupmenu2_Callback(hObject, eventdata, handles)
contents= cellstr(get(hObject,'String'));
popchoice2= contents(get(hObject,'Value'));
if (strcmp(popchoice2,'a'))
pop2val=40;
elseif(strcmp(popchoice2,'b'))
pop2val=50;
elseif(strcmp(popchoice2,'c'))
pop2val=60;
end
set(handles.popupmenu2,'UserData',pop2val);
function pushbutton1_Callback(hObject, eventdata, handles)
pop1val=get(handles.popupmenu1,'UserData');
pop2val=get(handles.popupmenu2,'UserData');
c=pop1val*pop2val;
set(handles.edit1,'string',c);
Accept it if its work for you!!
Thanks
Ankit
  1 件のコメント
ANURAG DEEPAK
ANURAG DEEPAK 2019 年 9 月 16 日
Thanks very much sir.... the probelm is solved by solution 1.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by