input Request user input user_entry = input('prompt') user_entry = input('prompt', 's') Input - Output dialog commands and boxes The response to the input prompt can be any MATLAB expression, which is evaluated using the variables in the current workspace. Remarks user_entry = input('prompt') displays prompt as a prompt on the screen, waits for input from the keyboard, and returns the value entered in user_entry. user_entry = input('prompt', 's') returns the entered string as a text variable rather than as a variable name or numerical value. If you press the Return key without entering anything, input returns an empty matrix. The text string for the prompt can contain one or more '\n' characters. The '\n' means to skip to the next line. This allows the prompt string to span several lines. To display just a backslash, use '\\'. If you enter an invalid expression at the prompt, MATLAB displays the relevant error message and then prompts you again to enter input. Examples Press Return to select a default value by detecting an empty matrix: reply = input('do you want more? Y/N [Y]: ', 's'); if isempty(reply) reply = 'Y'; else reply = upper(reply); end disp(reply); reply = input('pierwszy wiersz \n Drugi wiersz\n Y/N [Y]: ', 's'); if isempty(reply) reply = 'Y'; else reply = upper(reply); end disp(reply);
inputdlg Create and open input dialog box answer = inputdlg(prompt) creates a modal dialog box and returns user input. answer = inputdlg('prompt'); answer_num = str2num(answer{1}); disp(answer); disp(answer_num); For multiple prompts in the cell array. prompt is a cell array containing prompt strings. answer = inputdlg({'prompt1', 'prompt2', 'prompt3'}); for i=1:3 answer_num(i) = str2num(answer{i}); end; disp(answer_num'); answer = inputdlg(prompt,dlg_title) dlg_title specifies a title for the dialog box. answer = inputdlg({'prompt1', 'prompt2', 'prompt3'}, 'Tytuł okna dialogowego'); for i=1:3 answer_num(i) = str2num(answer{i}); end; disp(answer_num ); answer = inputdlg(prompt,dlg_title,num_lines) num_lines specifies the number of lines for each value. If num_lines is a scalar, it applies to all prompts. If num_lines is a column vector, each element specifies the number of lines of input for a prompt. answer = inputdlg({'prompt1', 'prompt2', 'prompt3'}, 'Tytuł okna dialogowego', 2); for i=1:3 answer_num(i) = str2num(answer{i}); end; disp(answer_num); answer = inputdlg(prompt,dlg_title,num_lines,defans) defans specifies the default value to display for each prompt. defans must contain the same number of elements as prompt and all elements must be strings. prompt_tab = {'prompt1', 'prompt2', 'prompt3'}; start_value_tab = {'111', '222', '333'}; answer = inputdlg(prompt_tab, 'Tytuł okna dialogowego', 2, start_value_tab); for i=1:3 answer_num(i) = str2num(answer{i}); end; disp(answer_num);
answer = inputdlg(prompt, dlg_title, num_lines, defans, options) If options is the string 'on', the dialog is made resizable in the horizontal direction. If options is a structure, the fields shown in the following table are recognized: Field Resize WindowStyle Interpreter Can be 'on' or 'off' (default). If 'on', the window is resizable horizontally. Can be either 'normal' or 'modal' (default). Can be either 'none' (default) or 'tex'. If the value is 'tex', the prompt strings are rendered using LaTeX. prompt_tab = {'prompt1', 'prompt2', 'prompt3'}; start_value_tab = {'111', '222', '333'}; options.resize='on'; answer = inputdlg(prompt_tab, 'Tytuł okna dialogowego', 2, start_value_tab, options); for i=1:3 answer_num(i) = str2num(answer{i}); end; disp(answer_num); If the user clicks the Cancel button to close an inputdlg box, the dialog returns an empty cell array: answer = {}
msgbox Create and open message box h = msgbox(message) h = msgbox(message,title) h = msgbox(message,title,icon) h = msgbox(message,title,'custom',icondata,iconcmap) h = msgbox(...,createmode) h = msgbox(message) creates a message dialog box that automatically wraps Message to fit an appropriately sized figure. Message is a string vector, string matrix, or cell array. msgbox returns the handle of the message box in h. h = msgbox(message,title) specifies the title of the message box. h = msgbox(message,title,icon) specifies which icon to display in the message box. Icon is 'none', 'error', 'help', 'warn', or 'custom'. The default is 'none'. Message = 'Wiadomość'; Title = 'Tytuł' Icon = 'help'; h = msgbox(message,title,icon); h = msgbox(message,title,'custom',icondata,iconcmap) defines a customized icon. IconData contains image data defining the icon. IconCMap is the colormap used for the image. h = msgbox(...,createmode) specifies whether the message box is modal or nonmodal. Optionally, it can also specify an interpreter for Message and Title. If CreateMode is a string, it must be one of the values shown in the following table. CreateMode Value 'modal' 'non-modal' (default) 'replace' Replaces the message box having the specified Title, that was last created or clicked on, with a modal message box as specified. All other message boxes with the same title are deleted. The message box which is replaced can be either modal or nonmodal. Creates a new nonmodal message box with the specified parameters. Existing message boxes with the same title are not deleted. Replaces the message box having the specified Title, that was last created or clicked on, with a nonmodal message box as specified. All other message boxes with the same title are deleted. The message box which is replaced can be either modal or nonmodal.
If CreateMode is a structure, it can have fields WindowStyle and Interpreter. The WindowStyle field must be one of the values in the table above. Interpreter is one of the strings'tex' or 'none'. The default value for Interpreter is 'none'. questdlg Create and open question dialog box button = questdlg('qstring') button = questdlg('qstring','title') button = questdlg('qstring','title',default) button = questdlg('qstring','title','str1','str2',default) button = questdlg('qstring','title','str1','str2','str3',default) button = questdlg('qstring','title',..., options) button = questdlg('qstring') displays a modal dialog box presenting the question 'qstring'. The dialog has three default buttons, Yes, No, and Cancel. If the user presses one of these three buttons, button is set to the name of the button pressed. If the user presses the close button on the dialog without making a choice, button is set to the empty string. If the user presses the Return key, button is set to 'Yes'. 'qstring' is a cell array or a string that automatically wraps to fit within the dialog box. Example 1 button = questdlg('qstring','title') displays a question dialog with 'title' displayed in the dialog's title bar. button = questdlg('qstring','title',default) specifies which push button is the default in the event that the Return key is pressed. 'default' must be 'Yes', 'No', or 'Cancel'. button = questdlg('qstring','title','str1','str2',default) creates a question dialog box with two push buttons labeled 'str1' and 'str2'. default specifies the default button selection and must be 'str1' or 'str2'. button = questdlg('qstring','title','str1','str2','str3',default) creates a question dialog box with three push buttons labeled 'str1', 'str2', and 'str3'. default specifies the default button selection and must be 'str1', 'str2', or 'str3'. When default is specified, but is not set to one of the button names, pressing the Enter key displays a warning and the dialog remains open. button = questdlg('qstring','title',..., options) replaces the string default with a structure, options. The structure specifies which button string is the default answer, and whether to use TeX to interpret the question string, qstring. Button strings and dialog titles cannot use TeX interpretation. The options structure must include the fields Default and Interpreter, both strings. It can include other fields, but questdlg does not use them. You can set Interpreter to 'none' or 'tex'. If the Default field does not contain a valid button name, a command window warning is issued and the dialog box does not respond to pressing the Enter key. Create a dialog that requests a dessert preference and encode the resulting choice as an integer. % Construct a questdlg with three options choice = questdlg('please choose a dessert:', 'Dessert Menu', 'Ice cream', 'Cake', 'No thank you', 'No thank you'); % Handle response switch choice case 'Ice cream'
disp([choice ' coming right up.']) dessert = 1; break case 'Cake' disp([choice ' coming right up.']) dessert = 2; break case 'No thank you' disp('i''ll bring you your check.') dessert = 0; end The case statements can contain white space but are case-sensitive. pause Halt execution temporarily pause pause(n) pause on pause, by itself, causes the currently executing M-file to stop and wait for you to press any key before continuing. Pausing must be enabled for this to take effect. pause(n) pauses execution for n seconds before continuing, where n can be any nonnegative real number. The resolution of the clock is platform specific. A fractional pause of 0.01 seconds should be supported on most platforms. Pausing must be enabled for this to take effect. keyboard Input from keyboard keyboard keyboard, when placed in an M-file, stops execution of the file and gives control to the keyboard. The special status is indicated by a K appearing before the prompt. You can examine or change variables; all MATLAB commands are valid. This keyboard mode is useful for debugging your M-files.. To terminate the keyboard mode, type the command return then press the Return key.
uigetfile Open standard dialog box for retrieving files uigetfile [FileName,PathName,FilterIndex] = uigetfile(filterspec) [FileName,PathName,FilterIndex] = uigetfile(filterspec,dialogtitle) [FileName,PathName,FilterIndex] = uigetfile(filterspec,dialogtitle,defaultname) [FileName,PathName,FilterIndex] = uigetfile(...,'multiselect',selectmode) uigetfile displays a modal dialog box that lists files in the current directory and enables the user to select or type the name of a file to be opened. If the filename is valid and if the file exists, uigetfile returns the filename when the user clicks Open. Otherwise uigetfile displays an appropriate error message from which control returns to the dialog box. The user can then enter another filename or click Cancel. If the user clicks Cancel or closes the dialog window, uigetfile returns 0. If FilterSpec is a string that contains a filename, the filename is displayed and selected in the File name field and the file's extension is used as the default filter. If FilterSpec is a cell array of strings, the first column contains a list of file extensions. The optional second column contains a corresponding list of descriptions. These descriptions replace standard descriptions in the Files of type field. A description cannot be an empty string. If FilterSpec is not specified, uigetfile uses the default list of file types (i.e., all MATLAB files). After the user clicks Open and if the filename exists, uigetfile returns the name of the file in FileName and its path in PathName. If the user clicks Cancel or closes the dialog window, FileName and PathName are set to 0. FilterIndex is the index of the filter selected in the dialog box. Indexing starts at 1. If the user clicks Cancel or closes the dialog window, FilterIndex is set to 0. [FileName,PathName,FilterIndex] = uigetfile(...,'multiselect',selectmode) sets the multiselect mode to specify if multiple file selection is enabled for the uigetfile dialog. Valid values for selectmode are 'on' and 'off' (default). If 'MultiSelect' is 'on' and the user selects more than one file in the dialog box, then FileName is a cell array of strings, each of which represents the name of a selected file. Filenames in the cell array are in the sort order native to your platform. Because multiple selections are always in the same directory, PathName is always a string that represents a single directory. Example 1 The following statement displays a dialog box that enables the user to retrieve a file. The statement lists all MATLAB M- files within a selected directory. The name and path of the selected file are returned in FileName and PathName. Note that uigetfile appends All Files(*.*) to the file types when FilterSpec is a string. [FileName,PathName] = uigetfile('*.m','select the M-file');
Example 2 To create a list of file types that appears in the Files of type list box, separate the file extensions with semicolons, as in the following code. Note that uigetfile displays a default description for each known file type, such as "Simulink Models" for.mdl files. [filename, pathname] = uigetfile({'*.m';'*.mdl';'*.mat';'*.*'},'file Selector'); Example 3 If you want to create a list of file types and give them descriptions that are different from the defaults, use a cell array, as in the following code. This example also associates multiple file types with the 'MATLAB Files' description. [filename, pathname] = uigetfile(... {'*.m;*.fig;*.mat;*.mdl','matlab Files (*.m,*.fig,*.mat,*.mdl)'; '*.m', 'M-files (*.m)';... '*.fig','figures (*.fig)';... '*.mat','mat-files (*.mat)';... '*.mdl','models (*.mdl)';... '*.*', 'All Files (*.*)'},... 'Pick a file'); The first column of the cell array contains the file extensions, while the second contains the descriptions you want to provide for the file types. Note that the first entry of column one contains several extensions, separated by semicolons, all of which are associated with the description 'MATLAB Files (*.m,*.fig,*.mat,*.mdl)'. The code produces the dialog box shown in the following figure.
Example 4 The following code checks for the existence of the file and displays a message about the result of the open operation. [filename, pathname] = uigetfile('*.m', 'Pick an M-file'); if isequal(filename,0) disp('user selected Cancel') else disp(['user selected', fullfile(pathname, filename)]) end Example 5 This example creates a list of file types and gives them descriptions that are different from the defaults, then enables multiple file selection. The user can select multiple files by holding down the Shift or Ctrl key and clicking on a file. [filename, pathname, filterindex] = uigetfile(... { '*.mat','mat-files (*.mat)';... '*.mdl','models (*.mdl)';... '*.*', 'All Files (*.*)'},... 'Pick a file',... 'MultiSelect', 'on'); sound Play vector as sound. SOUND(Y,FS) sends the signal in vector Y (with sample frequency FS) out to the speaker on platforms that support sound. Values in Y are assumed to be in the range -1.0 <= y <= 1.0. Values outside that range are clipped. Stereo sounds are played, on platforms that support it, when Y is an N-by-2 matrix. SOUND(Y) plays the sound at the default sample rate of 8192 Hz. Example: load handel sound(y,fs) You should hear a snippet of Handel's Hallelujah Chorus.