在MATLAB中将数据写入文本文件
在MATLAB中,有一个内置函数“fprintf()”用于将数据写入文本文件。此函数的优点在于它可以根据指定的格式将数据写入文本文件。
语法
fprintf(fileID, FormateSpecifier, Variables);
MATLAB中的格式说明符
下表列出了MATLAB中“fprintf()”函数使用的不同格式说明符
| 序号 | 格式说明符 | 描述 |
|---|---|---|
| 1. | %d 或 %i | 将值显示为整数。 |
| 2. | %f | 显示浮点数。 |
| 3. | %s | 显示字符串数组或字符向量。 |
| 4. | %e | 以指数形式显示值。 |
| 5. | %c | 显示单个字符。 |
| 6. | %g | 显示没有尾随零的值。 |
MATLAB中的转义序列
下表列出了MATLAB中“fprintf()”函数使用的不同转义序列
| 序号 | 转义序列 | 描述 |
|---|---|---|
| 1. | 创建新行。 | |
| 2. | \t | 插入水平制表符。 |
| 3. | \v | 插入垂直制表符。 |
| 4. | \r | 回车。 |
| 5. | \ | 反斜杠 |
| 6. | \b | 退格。 |
| 7. | \f | 换页。 |
| 8. | %% | 百分号字符 |
| 9. | \a | 报警 |
| 10. | '' | 单引号 |
算法
步骤1 - 将您的数据存储在变量或变量中。
步骤2 - 打开一个文件以将数据写入其中。为此,请使用“fopen()”函数。
步骤3 - 检查文件是否已成功打开。如果不是,则显示错误消息。为此,请将文件标识符与-1进行比较。
步骤4 - 将数据写入文本文件。为此,请使用“fprintf()”函数。
步骤5 - 关闭文件以释放系统资源。为此,请使用“fclose()”函数。
步骤6 - 向用户显示进程完成消息。
现在,让我们通过几个示例程序来了解在MATLAB中将数据写入文本文件的过程。
示例
% 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.');
输出
Data has been written to the text file.
通过编译以下语句查看文本文件的内容
disp('Data in the File:');
type TextFile1.txt;
解释
在这个MATLAB程序中,我们创建了一个包含整数的数组“x”。我们想将此数据写入名为“TextFile1.txt”的文本文件。
为此,我们首先使用“w”作为访问模式调用“fopen”函数,以写入模式打开文件。为了引用打开的文件,我们使用文件标识符“file1”。然后,我们检查文件是否成功打开,如果文件已打开,则显示错误消息。之后,我们使用“%d”作为格式说明符调用“fprintf”函数,以将整数数据写入文本文件。然后,我们调用“fclose”函数关闭文件以释放资源。最后,我们显示一条消息,通知数据已成功写入文件。
此外,要查看写入文件的数据,我们执行语句“type TextFile1.txt”。
示例
% 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.');
输出
Data has been written to the text file.
通过编译以下语句查看文本文件的内容
disp('Floating Point Numbers in the File:');
type FloatingPoint.txt;
解释
在这个MATLAB程序中,我们创建了一个包含浮点数的数组“x”。我们想将此数据写入名为“FloatingPoint.txt”的文本文件。
为此,我们使用“w”作为访问模式调用“fopen”函数,以写入模式打开文件。为了引用打开的文件,我们使用文件标识符“file2”。然后,我们检查文件是否成功打开,如果文件已打开,则显示错误消息。之后,我们使用“%f”作为格式说明符调用“fprintf”函数,以将数据写入文本文件。然后,我们调用“fclose”函数关闭文件以释放资源。最后,我们显示一条消息,通知数据已成功写入文件。
要查看写入文件的数据,我们执行语句“type FloatingPoint.txt”。
示例
% 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.');
输出
Data has been written to the text file.
通过编译以下语句查看文本文件的内容
disp('String in the text file is ');
type MyFile.txt;
示例
% 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.');
输出
执行以下代码以获取结果
type cubepower.txt;
示例
% A MATLAB program to write a hyperlink to a text file.
% Specify the website url.
url = 'https://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.');
输出
执行以下代码以获取结果
type tutorialspoint.txt;
结论
这就是在MATLAB中将数据写入文本文件的方法。上述MATLAB程序说明了在MATLAB中使用“fprintf()”函数将数据写入文本文件的实现。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP