Input/Output and File Statements in MATLAB

Leave a Comment
MATLAB

Input / Output is possible in various forms in MATLAB. There are numerous functions for sending data to the Standard output device (monitor) and for reading data from the standard input device (keyboard). 
In MATLAB, there are three methods to assign a value to a variable:
Using the assignment (=) statement
Read the data from a file stored in the system
Take the value as an input from the user

Input() Function
To accept data from the user, one can use input() function.

R = input(‘Enter the Radius of the circle: ‘);


When this statement is executed, the string ‘Enter the Radius of the circle: ‘ is printed on the screen and the system waits for the user to enter a value. The data accepted is stored in the variable R. Notice, the type of the variable R is not specified. R can take any value valid in MATLAB, it can be character, integer, floating point number, string, or even an empty value (if the user presses enter without entering any value).


Menu() Function

This function is used to give the user an option to select his or her preferred choice from a window. The choice of the user returns a particular scalar value to the program. To create such a window,


X = menu(‘Title’, ‘choice – 1’, ‘choice – 2’, ….., ‘choice – n’);


Here, the value of the choice made by the user will be stored in the variable x. When executed, this command will create a menu and the user shall be able to enter his or her choice by mouse or by keyboard.

Output functions

Any non-integer value in MATLAB is displayed up to four digits after decimal point. For example, if we give

X = 4
Y = 200.0201

The output will be –

X = 4
Y = 200.02


FILE OPERATIONS

File opening

A file is selected by choosing a unique id called file-id. This id is assigned to a file while it is opened. This is used to perform operations like reading data from the file and writing data on the file. In MATLAB, Standard Output has fid 1 and Standard Error has fid 2. Hence, any file id that we allot will start from the value 3. To open a file, we use the command fopen().

F1 = fopen(‘filename.txt’,’r’);

Here,
F1 is the file id
filename.txt’ is the name of the file. (It can also be a .dat file)
‘r’ is the basic mode in which the file is opened. ‘r’ signifies read mode.

The various modes in which a file can be opened are given below:

MODE
MEANING
r
Open an existing file for reading. Error is displayed if file does not exist.
r+
Open the existing file for reading and writing. Error if file doesn’t exist.
w
Open a file for writing. If file already exists, its contents are deleted
w+
Open a file for writing and reading. If file already exists, its contents are deleted
a
Open an existing file for writing. Append to end of file
a+
Open an existing file for reading and writing. Append to end of file



Reading Data from Files
To read from text files, first we open the file using fopen() function, and then the fscanf()  function can be used to read data from the file.


For example,

F1 = fopen(‘filename.txt’, ‘r’);
X = fscanf( F1, ‘%s’);

This will store the data read from the file into the variable X.
F1 is the file id.
‘filename.txt’ is the name of the text file that we will be reading from.
‘r’ specifies the mode of operation on the file.
X is the variable at which the data read will be stored.
F1 refers to the file, and
%c is for the string to be read from the file.

Another function to read data from a file is the fgets() function. This is used to read a line from the file, including the newline (‘\n’) character.

X = fgets(F1);

Where X is a character string, and
F1 is the file id

Writing Data on files
The function fprintf() is used to print data onto a file. The syntax for fprintf() is –

fprintf( file_id, format, data);

If the file id is not specified, it is assumed to be 1, and file_id 1 is for Standard Ourpur Device. Hence the data will be printed to the monitor screen.

For example,

fprintf(‘abc.txt’, ‘My Name Is %s’, “XYZ”);

Here, the string “My Name Is XY” gets printed onto the file abc.txt.

fprintf(‘Welcome to PseudoBit’);

Here, the string “Welcome to PseudoBit” is printed on the Standard Output Device.


MATLAB




0 comments:

Post a Comment

Powered by Blogger.