How to create a drop down list with fixed title?

I want a drop box with title 'Math' under which basic mathematical operations will be there-
BasicOperation = uicontrol(parent, 'Style', 'popup', 'Units', 'normalized', 'ToolTipString', 'Add/Subtract', 'String',{' = ',' + ',' - ',' * ',' / '} );
Like in this image - Title 'MATLAB Central' under which some options are there!
Thanks!

2 件のコメント

Adam
Adam 2018 年 2 月 14 日
A dropdown list in a panel with a title, or simply with a text box above it would be the simplest option.
Nipurn Gulgulia
Nipurn Gulgulia 2018 年 2 月 15 日
編集済み: Nipurn Gulgulia 2018 年 2 月 15 日
@Adam I am doing it in panel only, but i have 5-6 different drop down button. I can't give a single title. Is there any other type of button which i can use?

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

 採用された回答

Rik
Rik 2018 年 2 月 14 日

0 投票

The code below stores the chosen option in handles.BasicOperationString and resets the value back to the first option. You need to add something somewhere to control for the case that the user selects the first option (i.e. the explanatory text).
handles.BasicOperation = uicontrol(...
'Parent',parent,...
'Style', 'popup',...
'Units', 'normalized',...
'ToolTipString', 'Add/Subtract',...
'String',{'Basic operations',' = ',' + ',' - ',' * ',' / '},...
'Callback',@(hObject,eventdata) BasicOperationCallback(hObject,eventdata,guidata(hObject)));
function BasicOperationCallback(hObject,eventdata,handles)%#ok
val=get(hObject,'Value');
str=get(hObject,'String');
handles.BasicOperationString=str{val};
guidata(hObject,handles)%update handle struct
set(hObject,'Value',1);%reset to text
end

5 件のコメント

Nipurn Gulgulia
Nipurn Gulgulia 2018 年 2 月 15 日
編集済み: Nipurn Gulgulia 2018 年 2 月 15 日
Is there any other type of button which i can use instead of pop up!
Nipurn Gulgulia
Nipurn Gulgulia 2018 年 2 月 15 日
as.BasicOperation = uicontrol(as.parent, 'Style', 'popupmenu', 'Units', 'normalized', 'Position', [0.51, 0.907, 0.09, 0.06],'ToolTipString', 'Add/Subtract','String',{'Basic',' = ',' + ',' - ',' * ',' / '},'Callback',@(hObject,eventdata)as.addBasic);
function addBasic(as,source,~)
exist_strings1 = get(as.SignalEdit,'String');
All_Opterations = get( as.BasicOperation,'String');
SelectedValue1 = get( as.BasicOperation,'Value');
set(as.SignalEdit,'String',[exist_strings1, ' ', All_Opterations{SelectedValue1}]);
end
Rik
Rik 2018 年 2 月 15 日
You can write something different to popupmenu yourself, but it will be non-trivial. It is too much work to do it just for an answer here. You might take a look on the file exchange.
Btw, your callback is odd. The callback has two input by default: hObject and eventdata. You can use an anonymous function to extend this to automatically loading the struct that is saved in the guidata. This can be done with what I wrote.
Also, for readability it is better to keep every property you set on a single line, like I did in my code. That way it is much easier to keep track of what you have set and in what order.
Nipurn Gulgulia
Nipurn Gulgulia 2018 年 2 月 15 日
編集済み: Nipurn Gulgulia 2018 年 2 月 15 日
Thanks for your suggestions, I tried to run your code but i am getting error "Undefined function 'BasicOperationCallback' for input arguments of type 'double'."
Rik
Rik 2018 年 2 月 15 日
How did you save it? Because I see no reason why this would be the case.

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

その他の回答 (0 件)

カテゴリ

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

タグ

タグが未入力です。

質問済み:

2018 年 2 月 14 日

コメント済み:

Rik
2018 年 2 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by