Read Words in a File in Reverse Order in MATLAB


Consider a text file containing a sentence “I Study MATLAB from Tutorials Point.” Now, we want to read these words of the text file in the reverse order, the result will be “Point Tutorials from MATLAB Study I.”. In this article, we will explore the implementation of MATLAB program to read words in a text file in reverse order.

Algorithm: Read Words in a File in Reverse Order

In MATLAB, we have to follow the following steps to read the words in a file in reverse order:

Step 1 - Open the file for reading by calling the function “fopen()”. This function takes two arguments namely, file path and access mode. For “read mode”, “r” is used as the access mode. This function returns a file identifier “FileID” that is used to reference the opened file.

Step 2 - Check if the given file is opened successfully. For this compare the value of “fileID” with “-1”. If “FileID” is equal to “-1”, then an error will be shown that specifies the file could not be opened.

Step 3 - Use the function “fscan()” to read the content of the file. This function takes two arguments namely, the file identifier (“FileID”) and the format specifier (“%c”). Here, the format specifier “%c” is used to read characters from the file. This content of the opened file is then stored in a variable “content”.

Step 4 - Use the function “fclose()” to close the file to free up the system resources. This function takes the file identifier, i.e. “FileID” as the argument.

Step 5 - Use the function “strsplit()” to split the content of the file into words.

Step 6 - Use the function “flip()” to reverse the order of words.

Step 7 - Use the function “disp()” to display the reversed words.

Now let us consider a MATLAB program example to understand how to read words in a file in reverse order.

Example 

% MATLAB program to demonstrate read words in a file in reverse order
% Create a text file
str = 'I Study from Tutorials Point';
FileID = fopen('tutorials_point.txt','w');
fprintf(FileID,'%s',str);
fclose(FileID);
% Open the file for reading its content
FileID = fopen('tutorials_point.txt', 'r');
% Check for successful file opening
if FileID == -1
   error('Failed to open the file.');
end
% Read the file content
content = fscanf(FileID, '%c');
% Close the file to free up resources
fclose(FileID);
% Split the file content into words
words = strsplit(content);
% Reverse the order of words
reversed_words = flip(words);
% Display the reversed words
disp('Words in the Reverse Order:')
disp(reversed_words)

Output

Words in the Reverse Order:
{
  [1,1] = Point
  [1,2] = Tutorials
  [1,3] = from
  [1,4] = Study
  [1,5] = I
}

Explanation

In the above MATLAB program, we start by calling the function “fopen” with the access mode “w” to create a text file. Then, we call the function “fopen” with “r” to open the file for reading its content. After that we check whether the file is opened successfully or not. If the file is not open, then it shows an error.

We call the function “fscanf” with “%c” as the format specifier to read its content. Then, we call the function “fclose” to close the file to free up the system resources. Now, we call the function “strsplit” to split the content in words and it stores these words in a variable “words”. After that we call the function “flip” to reverse the order of words. Finally, we call the function “disp” to display the reversed words.

Conclusion

To perform this operation, MATLAB has a function “flip()” that reverses the order of words input to it. The above example program in MATLAB demonstrates the reversal of order of words in a file.

Updated on: 18-Jul-2023

68 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements