0% found this document useful (0 votes)
1K views4 pages

MATLAB Input Dialog Box Guide

The inputdlg function in MATLAB creates a modal dialog box that allows users to enter multiple text inputs. It returns the inputs as a cell array that can be used as variables. The function syntax allows specifying prompts, titles, number of input lines, default values, and options like making the dialog resizable. Examples demonstrate getting integer and string inputs, converting the results to numbers, and setting options for the dialog box.

Uploaded by

whytherito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views4 pages

MATLAB Input Dialog Box Guide

The inputdlg function in MATLAB creates a modal dialog box that allows users to enter multiple text inputs. It returns the inputs as a cell array that can be used as variables. The function syntax allows specifying prompts, titles, number of input lines, default values, and options like making the dialog resizable. Examples demonstrate getting integer and string inputs, converting the results to numbers, and setting options for the dialog box.

Uploaded by

whytherito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

30/11/2014

CreateandopeninputdialogboxMATLABinputdlg

inputdlg
Create and open input dialog box

Syntax
answer=inputdlg(prompt)
answer=inputdlg(prompt,dlg_title)
answer=inputdlg(prompt,dlg_title,num_lines)
answer=inputdlg(prompt,dlg_title,num_lines,defAns)
answer=inputdlg(prompt,dlg_title,num_lines,defAns,options)

Description
answer=inputdlg(prompt)createsamodaldialogboxandreturnsuserinputformultiplepromptsinthe
[Link].

[Link]
moreinformation,seeWindowStyleinFigureProperties.
answer=inputdlg(prompt,dlg_title)dlg_titlespecifiesatitleforthedialogbox.
answer=inputdlg(prompt,dlg_title,num_lines)num_linesspecifiesthenumberoflinesforeachuser
enteredvalue.num_linescanbeascalar,columnvector,oramx2array.

Ifnum_linesisascalar,itappliestoallprompts.
Ifnum_linesisacolumnvector,eachelementspecifiesthenumberoflinesofinputforaprompt.
Ifnum_linesisanarray,itmustbesizemby2,wheremisthenumberofpromptsonthedialogbox.
[Link]
secondcolumnspecifiesthewidthofthefieldincharacters.
answer=inputdlg(prompt,dlg_title,num_lines,defAns)defAnsspecifiesthedefaultvaluetodisplayfor
[Link]

strings.
answer=inputdlg(prompt,dlg_title,num_lines,defAns,options)Ifoptionsisthestring'on',thedialog

[Link],thefieldsshowninthefollowingtable
arerecognized:
Field

Description

Resize

Canbe'on'or'off'(default).If'on',thewindowisresizablehorizontally.

WindowStyle

Canbeeither'normal'or'modal'(default).

Interpreter

Canbeeither'none'(default)or'tex'.Ifthevalueis'tex',thepromptstringsare
renderedusingLaTeX.

IftheuserclickstheCancelbuttontocloseaninputdlgbox,thedialogreturnsanemptycellarray:

answer=
{}
[Link]

1/4

30/11/2014

CreateandopeninputdialogboxMATLABinputdlg

Examples
Example1
[Link].

prompt={'Entermatrixsize:','Entercolormapname:'};
dlg_title='Input';
num_lines=1;
def={'20','hsv'};
answer=inputdlg(prompt,dlg_title,num_lines,def);

Example2
[Link]
string,soconvertthestringtonumbersusingstr2num.

x=inputdlg('Enterspaceseparatednumbers:',...
'Sample',[150]);
data=str2num(x{:});

Example3
Createadialogboxtodisplayinputfieldsofdifferentwidths.

x=inputdlg({'Name','Telephone','Account'},...
'Customer',[150;112;17]);

[Link]

2/4

30/11/2014

CreateandopeninputdialogboxMATLABinputdlg

Example4
[Link],usetheoptionstomakeitresizableandnotmodal,and
tointerpretthetextusingLaTeX.

prompt={'Enterthematrixsizeforx^2:',...
'Enterthecolormapname:'};
name='InputforPeaksfunction';
numlines=1;
defaultanswer={'20','hsv'};
answer=inputdlg(prompt,name,numlines,defaultanswer);

[Link]='on';
[Link]='normal';
[Link]='tex';
answer=inputdlg(prompt,name,numlines,...
defaultanswer,options);

[Link]

3/4

30/11/2014

CreateandopeninputdialogboxMATLABinputdlg

MoreAbout
collapseall
Tips
inputdlgusestheuiwaitfunctiontosuspendexecutionuntiltheuserresponds.

Thereturnedvariableanswerisacellarraycontainingstrings,onestringpertextentryfield,startingfrom
thetopofthedialogbox.
Toconvertamemberofthecellarraytoanumber,[Link],youcanaddthefollowing
codetotheendofanyoftheexamplesbelow:

%Usecurlybracketforsubscript
[valstatus]=str2num(answer{1});
if~status
%Handleemptyvaluereturned
%forunsuccessfulconversion
%...
end
%valisascalarormatrixconvertedfromthefirstinput
Userscanenterscalarorvectorvaluesintoinputdlgfieldsstr2numconvertsspaceandcomma
delimitedstringsintorowvectors,[Link],if
answer{1}contains'123;456+7i',theconversionproduces:

val=str2num(answer{1})
val=
1.00002.00003.0000
4.00005.00006.0000+7.0000i

SeeAlso
dialog|errordlg|figure|helpdlg|input|listdlg|msgbox|questdlg|str2num|uiresume|uiwait|
warndlg

[Link]

4/4

You might also like