Request Numeric Input or Expression Using MATLAB Input Function
The input function in MATLAB is used to prompt the user for input and allows the user to enter a value or text which can be stored in a variable for further use in the program. The input function supports various data types, including numbers, characters, and logical values.
To utilize the input function, we can follow these steps:
- Use the disp function to display a message or prompt to the user.
- Call the input function and create a variable that stores the user response.
- Optionally, validate the user’s input to ensure it meets the required criteria.
Example
Below MATLAB code uses the input function and prompts the user to enter a value. The user-entered value is stored in variable x.
The code then performs a calculation where the value of x is multiplied by 10, and the result is stored in the variable y.
x = input(prompt)
y = x*10
Once the code is run enter any value in the command window:
The MATLAB input function can also take expressions input from users.
For example, rerun the above code and type magic(3) in the command window:
x = input(prompt)
y = x*10
After running type magic(3) in command window:
This code calls the magic(3) function, which generates a 3×3 magic square and displays it on the screen.
Here’s another example of using the input function to ask for a user’s name:
name = input('Name: ', 's');
disp(['Hello, ' name '! Welcome to the program.']);
In the above MATLAB code, the disp function displays the prompt asking the user to enter their name. The input function then waits for the user’s input, which is stored in the variable name. The argument passed to the input function is a string, so the input will be taken as a string. The program uses the disp function again to greet the user by name.
Request Unprocessed Text Input
Now we will create a simple request-response to take text input from users using the MATLAB code.
questions = {'Do you know MATLAB? Y/N [Y]: ', 'Have you used MATLAB for data analysis? Y/N [Y]: ', 'Do you enjoy programming in MATLAB? Y/N [Y]: '};
answers = cell(size(questions));
% Ask questions
for i = 1:numel(questions)
prompt = questions{i};
txt = input(prompt, 's');
% Check if the user provided an answer or use the default value
if isempty(txt)
txt = 'Y';
end
answers{i} = txt;
end
% Display answers
disp('--- Answers ---');
for i = 1:numel(questions)
disp([questions{i} answers{i}]);
end
This MATLAB code initializes variables and asks the user a series of questions using the MATLAB input function. The questions are stored in a cell array called questions. The user’s answers are stored in another cell array called answers.
The code uses a loop to iterate through each question. It displays the current question using the disp function and prompts the user for an answer using the input function. This input is stored as a string in the variable txt.
After getting the user’s input, the code checks if the input is empty. If the user did not provide an answer and left it empty, the code assigns a default value of Y (indicating yes) to the variable txt. All the user’s answers are printed on the command window.
Creating Interactive Dialog Boxes Using inputdlg Function
In addition to the input function, MATLAB provides a convenient way to create interactive dialog boxes using the inputdlg function. Dialog boxes offer a more visually appealing and structured way to gather user input. They allow us to present multiple input fields, labels, and default values to the user.
To create a dialog box using the inputdlg function, follow these steps:
- Define a cell array of prompt strings to specify the information we need from the user.
- Optionally, provide a cell array of default values to prepopulate the input fields.
- Call the inputdlg function with the prompt strings and default values to create the dialog box.
- Retrieve the user’s input from the output of the inputdlg function.
Here’s an example of creating a dialog box to ask for the user’s age and favorite color:
defaults = {'25', 'blue'};
answers = inputdlg(prompts, 'User Information', 1, defaults);
age = str2double(answers{1});
color = answers{2};
disp(['You are ' num2str(age) ' years old and your favorite color is ' color '.']);
In the above-given code, the prompts variable contains the prompt strings for age and favorite color. The defaults variable provides default values for the input fields, which are set to 25 and blue respectively. The inputdlg function is called with the prompts, a title for the dialog box (“User Information”), the number of input fields (1), and the defaults.
The user’s responses are stored in the answers cell array. In the end, the program extracts the age and color values from the answers array and displays them using the disp function.
After compiling the code, we will get the following dialogue box for user input. After filling in the data click OK.
Once the data is entered, the following output appears in the MATLAB command window.
Utilizing GUI Elements for Input
If you want to take user interaction to the next level, MATLAB provides a wide range of GUI (Graphical User Interface) elements that can be used to obtain input from users. These GUI components include sliders, buttons, checkboxes, dropdown menus, and more. By using these elements in MATLAB programs, we can create interactive interfaces.
To utilize GUI elements for input, follow these steps:
- Create a figure window using the figure function.
- Add the desired GUI components to the figure using functions such as uicontrol, uimenu, or the MATLAB App Designer.
- Specify the callback functions for each component to handle user input and trigger appropriate actions.
- Run the GUI program using the uiwait or waitfor function to enable user interaction.
Here’s a simple example program in MATLAB that utilizes GUI elements for input.
% Create the GUI window
fig = uifigure('Name', 'GUI Input Example', 'Position', [100 100 300 150]);
% Create a text box for input
txtInput = uitextarea(fig, 'Position', [50 80 200 40]);
% Create a button
btnSubmit = uibutton(fig, 'Position', [110 30 80 30], 'Text', 'Submit', 'ButtonPushedFcn', @(btnSubmit, event) submitButtonCallback(txtInput));
% Callback function for the button
function submitButtonCallback(txtInput)
inputText = txtInput.Value; % Get the text from the text box
disp(['Input text: ' inputText]); % Display the text in the Command Window
end
end
In the above MATLAB code, we will create a GUI window with a text box and a button. When the button is clicked, the program will get the text entered in the text box and display it in the MATLAB Command Window.
To run the program, simply call the guiInputExample function in the MATLAB Command Window. It will open a GUI window with a text box. Enter some text in the text box and click the Submit button. User input is displayed on Command Window.
Note: This example uses the new MATLAB UI components introduced in MATLAB R2018a and later versions.
After filling in the data we will see the same data printed on the command window.
Conclusion
Asking for input in MATLAB helps in creating interactive programs that ask input from users. This article covers various techniques for obtaining user input, including the input function, creating interactive dialog boxes, and utilizing GUI elements. All three methods are discussed along with examples. Input is the basic function for taking user input in MATLAB, further, we can create an interactive dashboard using the GUI MATLAB elements. Read more about taking MATLAB input from users in this article.