Creating a dropdown menu in inputdlg box in MATLAB

235 views (last 30 days)
I have this Matlab code:
prompt = {'Enter period:','Enter frequency:'};
dlgtitle = 'Input';
dims = [1 35];
definput = {'3000','10'};
answer = inputdlg(prompt,dlgtitle,dims,definput)
I want to create a dropdown menu for period only with specific values.
How can I do it?

Answers (2)

Anton Angov
Anton Angov on 21 Dec 2020
Edited: Anton Angov on 21 Dec 2020
Having a similar problem, and spending some time on research, I was able to find the following post from 2009:
Even that the post is quite old, if you follow up the inputsdlg() function link you will find that it has been updated and is working quite well. Instead of spending time to implement something with this functionality, in my case reusing this function is all I needed.
The zip file containing the function and some examples can be downloaded from here:
In your particular case, here is what I came up with:
%% Copyright (c) 2015, Takeshi Ikuma
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%%
name = 'Input';
prompt = {'Enter period:';'Enter frequency:'};
%%
formats = struct('type', {}, 'style', {}, 'items', {}, ...
'format', {}, 'limits', {}, 'size', {});
formats(1,1).type = 'list';
formats(1,1).style = 'popupmenu';
formats(1,1).items = {'0','1000', '2000', '3000', '4000'}; % edit the period values here
formats(2,1).type = 'edit';
formats(2,1).format = 'integer';
formats(2,1).size = [100 20];
defaultanswers = {4, 10};% 4 is an index from the items in the list, 10 is an integer value
[answer, canceled] = inputsdlg(prompt, name, formats, defaultanswers);
Producing the following output:
I hope that helps!

Rik
Rik on 26 Jul 2020
You can create new figure and put a uicontrol element on it, or you can use listdlg. If you need help implementing my first suggestion, don't hesitate to post a comment.

Categories

Find more on Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!