
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write Data to Text Files in MATLAB
In MATLAB, there is a built-in function "fprintf()" that is used to write data to a text file. The best part of this function is that it writes formatted data to a text file exactly in the same way as specified.
Syntax
fprintf(fileID, FormateSpecifier, Variables);
Format Specifiers in MATLAB
The following table gives a list of different format specifiers used with the "fprintf()" function in MATLAB:
S. No. | Format Specifier | Description |
---|---|---|
1. | %d or %i | Display a value as an integer. |
2. | %f | Display floating point number. |
3. | %s | Display a string array or a character vector. |
4. | %e | Display values in exponential notation. |
5. | %c | Display a single character. |
6. | %g | Display values with no trailing zeros. |
Escape Sequences in MATLAB
The following table gives a list of different escape sequences used with the "fprintf()" function in MATLAB:
S. No. | Escape Sequence | Description |
---|---|---|
1. | To create a new line. | |
2. | \t | To insert a horizontal tab space. |
3. | \v | To insert a vertical tab space. |
4. | \r | Carriage return. |
5. | \ | Backslash |
6. | \b | Backspace |
7. | \f | Form feed |
8. | %% | Percentage character |
9. | \a | Alarm |
10. | '' | Single quotation mark |
Algorithm
Step 1- Store your data in a variable or variables.
Step 2 - Open a file for writing data into it. For this use the "fopen()" function.
Step 3 - Check if the file was successfully opened. If not, then throw an error message. For this compare the file identifier with -1.
Step 4 - Write the data to the text file. For this use the "fprintf()" function.
Step 5 - Close the file to free up system resources. For this use the "fclose()" function.
Step 6 - Display a process completion message to the user.
Now, let us understand the process of writing up data to a text file in MATLAB with the help of a few example programs.
Example
% A MATLAB program to write multiple numeric values to a text file. % Create a sample array of integers. x = [10 20 30 40 50; 100, 200, 300, 400, 500; 1000, 2000, 3000, 4000, 5000]; % Open the file for writing file1 = fopen('TextFile1.txt', 'w'); % Check if the file was successfully opened if file1 == -1 error('Failed to open the file.'); end % Write data to the text file fprintf(file1, '%d %d %d
', x); % Close the file to free up system resources fclose(file1); disp('Data has been written to the text file.');
Output
Data has been written to the text file.
View the content of the text file by compiling the following statement:
disp('Data in the File:'); type TextFile1.txt;
Explanation
In this MATLAB program, we have created an array "x" containing integers. We want to write this data to a text file named "TextFile1.txt".
For this, first we call "fopen" function with "w" as the access mode to open the file in write mode. To refer the opened file, we use a file identifier "file1". Then, we check the file for successful opening, if the file is opened, an error message is shown. After that we call "fprintf" function with "%d" as format specifier to write the integer data to the text file. Then, we call "fclose" function to close the file to free up the resources. Finally, we display a message to inform that the data is successfully written to the file.
Also, to view the data written to the file, we execute a statement "type TextFile1.txt".
Example
% A MATLAB program to write floating point numeric values to a text file. % Create a sample array of floating point numbers. x = [10.25 20.52 30.56 40.45 50.65; 100.552, 200.235, 300.563, 400.456, 500.256; 1000.4256, 2000.2356, 3000.2356, 4000.2563, 5000.2356]; % Open the file for writing file2 = fopen('FloatingPoint.txt', 'w'); % Check if the file was successfully opened if file2 == -1 error('Failed to open the file.'); end % Write data to the text file fprintf(file2, '%f %f %f
', x); % Close the file to free up system resources fclose(file2); disp('Data has been written to the text file.');
Output
Data has been written to the text file.
View the content of the text file by compiling the following statement:
disp('Floating Point Numbers in the File:'); type FloatingPoint.txt;
Explanation
In this MATLAB program, we have created an array "x" containing floating point numbers. We want to write this data to a text file named "FloatingPoint.txt".
For this, we call "fopen" function with "w" as the access mode to open the file in write mode. To refer the opened file, we use a file identifier "file2". Then, we check the file for successful opening, if the file is opened, an error message is shown. After that we call "fprintf" function with "%f" as the format specifier to write the data to the text file. Then, we call "fclose" function to close the file to free up the resources. Finally, we display a message to inform that the data is successfully written to the file.
To view the data written to the file, we execute a statement "type FloatingPoint.txt".
Example
% A MATLAB program to write a string of text to a text file. % Create a sample string of text. x = 'Tutorials Point is a Best Online Platform to Learn Coding.'; % Open the file for writing file3 = fopen('MyFile.txt', 'w'); % Check if the file was successfully opened if file3 == -1 error('Failed to open the file.'); end % Write data to the text file fprintf(file3, '%s', x); % Close the file to free up system resources fclose(file3); disp('Data has been written to the text file.');
Output
Data has been written to the text file.
View the content of the text file by compiling the following statement:
disp('String in the text file is '); type MyFile.txt;
Example
% A MATLAB program to write data to a text file using a mathematical expression. % Create sample numeric data. x = 1:10; % Specify a mathematical expression y = [x; x.^3]; % Open the file for writing file4 = fopen('cubepower.txt', 'w'); % Check if the file was successfully opened if file4 == -1 error('Failed to open the file.'); end % Write data to the text file fprintf(file4, '%s \t \t %s
', 'x', 'cube'); fprintf(file4, '%f \t %f
', y); % Close the file to free up system resources fclose(file4); disp('Data has been written to the text file.');
Output
Execute the following code to get the result:
type cubepower.txt;
Example
% A MATLAB program to write a hyperlink to a text file. % Specify the website url. url = 'https://www.tutorialspoint.com/index.htm'; % Specify the name of the website website = 'Tutorials Point'; % Open the file for writing file5 = fopen('tutorialspoint.txt', 'w'); % Check if the file was successfully opened if file5 == -1 error('Failed to open the file.'); end % Write the hyperlink to the text file fprintf(file5, ' "%s"
', url, website); % Close the file to free up system resources fclose(file5); disp('Data has been written to the text file.');
Output
Execute the following code to get the result:
type tutorialspoint.txt;
Conclusion
This is all about writing data to a text file in MATLAB. The above MATLAB programs illustrate the implementation of "fprintf()" function in MATLAB to write data to a text file.