Displaying values in static box for values in pop up menu !!

조회 수: 11 (최근 30일)
chamant swarup
chamant swarup 2019년 5월 3일
댓글: Rik 2019년 5월 3일
Hello all !!
I am actually trying to create a GUI where i need to display certain values in static box for each chose value in pop up menu .
For example : When chosen 1 should display 1000 in static box and so on ,
i have written the following code in the call back of pop up menu with tag name popupmenu1 and staticbox with tag name text2 , the programs works fine but is not displaying the assigned values to static box !
Where am i going wrong ? Any sort of help is appreciated.
function popupmenu1_Callback(hObject, eventdata, handles)
a= get(handles.popupmenu1,'value');
if a==1
set(handles.text2,'value',1000);
end
if a==2
set(handles.text2,'value',1200);
end
if a==3
set(handles.text2,'value',3000);
end
if a==4
set(handles.text2,'value',4000);
end

채택된 답변

Rik
Rik 2019년 5월 3일
편집: Rik 2019년 5월 3일
There is a difference between the Value property and the String property. Also, you're better off using switch (which will make your code cleare), or a dictionary instead of a list of ifs.
function popupmenu1_Callback(hObject, eventdata, handles)
switch get(handles.popupmenu1,'Value');
case 1
set(handles.text2,'String',1000);
case 2
set(handles.text2,'String',1200);
case 3
set(handles.text2,'String',3000);
case 4
set(handles.text2,'String',4000);
end
Or with a dictionary:
function popupmenu1_Callback(hObject, eventdata, handles)
dict={1000,1200,3000,4000};
set(handles.text2,'String',dict(get(handles.popupmenu1,'Value')));
end
  댓글 수: 2
chamant swarup
chamant swarup 2019년 5월 3일
편집: chamant swarup 2019년 5월 3일
perfect thanks for ur insights ! but what is the diffrence i used value because the data in pop up menu is number ? doesnt that work that way ?
For suppose i need to display "consumption = 1000Kwh/annum" instead of "1000"
will that change ?
Rik
Rik 2019년 5월 3일
The Value property of a popupmenu describes the selected box. For other uicontrol types this meaning is different (for slider it returns the selected position, for radio it is the value of min when not selected and max when selected, ditto for checkbox, etc).
The String property is the string/char array that is displayed as the text of a uicontrol element. You can use something like the code below.
function popupmenu1_Callback(hObject, eventdata, handles)
dict={1000,1200,3000,4000};
str=sprintf('consumption = %.0fKwh/annum',...
dict(get(handles.popupmenu1,'Value')));
set(handles.text2,'String',str);
end

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by