A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.

Class

Class

Tuesday, May 10, 2016

Basic Data Input Commands in MATLAB

We have discussed about different types of variables in Matlab and different ways to assign data to them. Now we will discuss how to display the data stored in the variable to the command window or save it to a file. 

input

Input is the simple basic command to request the user input. It displays a prompt text for the user to understand what data to be entered and waits for the user to input a value and press the Return key. The user can enter numbers, expressions, or variables in the workspace. If the data required is a string, then it must be specified in the input command as shown in the syntax below.

Syntax
If the data is a numeric,             
x = input('prompt')
The ‘prompt’ is the message to be displayed in the command window. The entered data is stored to the variable x. If the user enters a non-numeric or an invalid expression, then MATLAB® displays the relevant error message, and then redisplays the prompt. If the user presses the Return key without entering anything, then input returns an empty matrix.

If the data is a string,     
str = input('prompt','s')
The entered text is stored to the variable str as a string

Input Dialog Box

Dialog box is another method to take the input from the user. Inputdlg is the command to create dialog box that gathers user input. The advantage of input dialog box is that it can gather multiple data in single step.

Syntax
Datain  = inputdlg('prompt')

Datain = inputdlg('prompt', 'title', num_lines, 'default', options)
The inputdlg command creates a dialog box for the user input and returns user input for multiple prompts.

Arguments other than prompt is optional. 'title' specifies a title for the dialog box, num_lines specifies the number of lines for each user input data and 'default' specifies the default value to display for each prompt. 'default' must contain the same number of elements as prompt and all elements must be of type char.
If the user clicks the Cancel button to close an inputdlg box, the dialog returns an empty cell array.

Examples
Example 1
Example 2
prompt = {'Enter Your Name:','Enter Your ID:'};
title = 'User Data';
num_lines = 1;
userdata = inputdlg(prompt,title,num_lines);
 
Example 3
Create a dialog box to display input fields of different widths.
x = inputdlg({'Name','Telephone','Account'},'Customer', [1 50; 1 12; 1 7]); 

sscanf

sscanf provides more control over reading data in the form of a string. It read all data as string and convert to the other formats based on the definition in the command.

Syntax
X = sscanf(str, format, size)
The sscanf(str, format) reads data from the vector str, converts it according to the format, and store the results in array X. Argument ‘size’ is optional. If not specified the function scan the string and pplies the format until the end of str or failing to match the format. If sscanf cannot match the format to the data, it reads only the portion that matches into X and stops processing. If str is a character array with more than one row, sscanf reads the characters in column order.

Examples
Example 1
Read multiple floating-point values from a character vector:
str = '1.234  3.456';

X = sscanf(str,'%f')
X =
    1.2340
    3.4560
Example 2
a= '1 2 3';

A = sscanf(a, '%d', 2)    % the size is defined as 2 and converted to double format.
A =
     1
     2


submit to reddit