如何编写与 MATLAB 中读取的格式相同的文本文件?

发布于 2024-08-15 09:57:15 字数 1442 浏览 1 评论 0原文

我过去曾问过相关问题感谢这里专家的帮助,我知道如何阅读该文件。现在我有一个新问题。我首先从文件中读取数据,如下所示:

fid = fopen('D:\file.txt', 'rt');
a = textscan(fid, '%s %f %f %f %f %f %f', ...
             'Delimiter',',', 'CollectOutput',1, 'HeaderLines',1);
fclose(fid);

然后处理文件并更改列的一些值。现在我想用与我的 file.txt 完全相同的格式和新值编写一个新文件 newfile.txt 。我该怎么做?

如果我执行以下操作:

M = [datenum(a{1}) a{2}];
dlmwrite('newfile1.txt', M);

它会为我提供一个新文件,没有我的第一行标题,也没有我想要的格式的第 1 列和第 2 列。

我的文件格式如下:

date        time,   M01, M02, M03, M04, M05, M06
8/15/2009, 0:00:00, 5.8, 7.8, 7.8, 7.3, 0, 7.9
8/15/2009, 0:10:00, 7.1, 8.1, 8.1, 7.7, 0, 8.1
8/15/2009, 0:20:00, 6.8, 7.4, 7.6, 7.1, 0, 7.3
8/15/2009, 0:30:00, 5.6, 6.8, 7.1, 6.6, 0, 6.8
8/15/2009, 0:40:00, 3.9, 6.2, 6.4, 6.2, 0, 6.4
8/15/2009, 0:50:00, 4.6, 5.5, 6.1, 5.8, 0, 5.6
8/15/2009, 1:40:00, 7, 7, 7.2, 6.9, 0, 6.3

我能够以以下格式创建一个新的 file.txt

我的文件格式如下:

                5.8, 7.8, 7.8, 7.3, 0, 7.9
                7.1, 8.1, 8.1, 7.7, 0, 8.1
                6.8, 7.4, 7.6, 7.1, 0, 7.3
                5.6, 6.8, 7.1, 6.6, 0, 6.8
                3.9, 6.2, 6.4, 6.2, 0, 6.4
                4.6, 5.5, 6.1, 5.8, 0, 5.6
                7, 7, 7.2, 6.9, 0, 6.3

有人可以帮我 2 将标题和前 2 列复制到这个新文件中吗?

I have asked a related question in the past and I know how to read the file thanks to the help of the experts here. Now I have a new problem. I first read the data from the file like so:

fid = fopen('D:\file.txt', 'rt');
a = textscan(fid, '%s %f %f %f %f %f %f', ...
             'Delimiter',',', 'CollectOutput',1, 'HeaderLines',1);
fclose(fid);

I then process the file and change a few values of the column. Now I want to write a new file newfile.txt in the exact same format as my file.txt with the new values. How do I do that?

If I do the following:

M = [datenum(a{1}) a{2}];
dlmwrite('newfile1.txt', M);

it gives me a new file without my first row of headers and without column 1 and column2 in the format I want.

My file format is given below:

date        time,   M01, M02, M03, M04, M05, M06
8/15/2009, 0:00:00, 5.8, 7.8, 7.8, 7.3, 0, 7.9
8/15/2009, 0:10:00, 7.1, 8.1, 8.1, 7.7, 0, 8.1
8/15/2009, 0:20:00, 6.8, 7.4, 7.6, 7.1, 0, 7.3
8/15/2009, 0:30:00, 5.6, 6.8, 7.1, 6.6, 0, 6.8
8/15/2009, 0:40:00, 3.9, 6.2, 6.4, 6.2, 0, 6.4
8/15/2009, 0:50:00, 4.6, 5.5, 6.1, 5.8, 0, 5.6
8/15/2009, 1:40:00, 7, 7, 7.2, 6.9, 0, 6.3

i am able to make a new file.txt in format

My file format is given below:

                5.8, 7.8, 7.8, 7.3, 0, 7.9
                7.1, 8.1, 8.1, 7.7, 0, 8.1
                6.8, 7.4, 7.6, 7.1, 0, 7.3
                5.6, 6.8, 7.1, 6.6, 0, 6.8
                3.9, 6.2, 6.4, 6.2, 0, 6.4
                4.6, 5.5, 6.1, 5.8, 0, 5.6
                7, 7, 7.2, 6.9, 0, 6.3

Can some one help me 2 copy the headers and the first 2 columns into this new file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唔猫 2024-08-22 09:57:15

注意:我已经更新了答案,以使用问题中指定的最新文件格式(即日期和时间值之间的逗号)。我还编写了下面的代码来处理非常大的文件,其中列数已知但行数可变。

首先,您必须使用以下更新的代码读取文件(这会节省使用函数 FGETS 的顶行)

fid = fopen('D:\file.txt','rt');  %# Open the file
topLine = fgets(fid);             %# Read the top line and store it in a string
data = textscan(fid,'%f','Delimiter',',/:');  %# Read the data
fclose(fid);                      %# Close the file

: ,您必须使用已知的数据列数(计算日期和时间列)来重塑data

N = 74;  %# Number of columns of data after the date and time
data = reshape(data{1},N+6,[])';

现在data是一个矩阵其中前六列包含日期和时间信息(月、日、年、小时、分钟和秒),所有其他数据位于剩余的 N 列中。如果您需要对日期和时间值执行任何操作,可以查看以下函数以了解如何将它们转换为不同的格式: DATENUMDATESTR

修改 data 中的值后,您可以使用 for 循环和 FPRINTF 函数:

fid = fopen('newfile1.txt','wt');  %# Open the file
fprintf(fid,'%s',topLine);         %# Print the top line
for i = 1:size(data,1)             %# Loop over the rows of data
  fprintf(fid,'%d/%d/%d, %d:%d:%d',data(i,1:6));  %# Print the date
  fprintf(fid,', %.1f',data(i,7:end));            %# Print the data
  fprintf(fid,'\n');                              %# Print a newline
end
fclose(fid);                       %# Close the file

我使用 86400-by-80 矩阵作为 data 运行上面的代码,它将数据写入文件大约需要 30 秒。

Note: I've updated the answer to work with the most current file format specified in the question (i.e. a comma between the date and time values). I've also written the code below to handle very large files where the number of columns are known but the number of rows is variable.

First, you'll have to read your file using the following updated code (which saves the top line using the function FGETS):

fid = fopen('D:\file.txt','rt');  %# Open the file
topLine = fgets(fid);             %# Read the top line and store it in a string
data = textscan(fid,'%f','Delimiter',',/:');  %# Read the data
fclose(fid);                      %# Close the file

Next, you have to reshape data using the known number of columns of data (not counting the date and time columns):

N = 74;  %# Number of columns of data after the date and time
data = reshape(data{1},N+6,[])';

Now data is a matrix where the first six columns contain date and time information (month, day, year, hours, minutes, and seconds) and all the other data is in the remaining N columns. If you need to do anything with the date and time values, you can look at the following functions to figure out how to convert them to different formats: DATENUM, DATESTR, and DATEVEC.

After you've modified the values in data you can resave it using a for loop and the FPRINTF function:

fid = fopen('newfile1.txt','wt');  %# Open the file
fprintf(fid,'%s',topLine);         %# Print the top line
for i = 1:size(data,1)             %# Loop over the rows of data
  fprintf(fid,'%d/%d/%d, %d:%d:%d',data(i,1:6));  %# Print the date
  fprintf(fid,', %.1f',data(i,7:end));            %# Print the data
  fprintf(fid,'\n');                              %# Print a newline
end
fclose(fid);                       %# Close the file

I ran the above code with an 86400-by-80 matrix for data and it took around 30 seconds to write the data to a file.

恏ㄋ傷疤忘ㄋ疼 2024-08-22 09:57:15

如您所知,dlmwrite 将变量(例如 M 数组)写入 ASCII 文本的文件中。检查该函数的文档,了解如何设置分隔符(以获取每行值之间的 , )以及如何附加到现有文件。

要写入标题行,我建议您设置一个字符串数组,填充它并使用 dlmwrite 将其写入输出文件。

现在,如果您想写入文件的其余部分(M 数组加上 2 个日期/时间的前导列),您将需要创建一个临时数组,其大小与 M 加上 2 个额外列相同。由于您的数据值是浮点数,并且您的日期和时间是某种类型的结构化数据(可能是字符串),我认为 Mtemp 必须是字符串数组。创建后,您可以使用单个 dlmwrite 语句将其附加到输出文件中。

请注意, num2str 按照您对向量的希望进行操作,但我不确定它是否可以被强制在值之间放入 , 。但是,它可以按照您指定的格式写入数字。

如果这没有给你想要的输出,或者你的数组太大而无法复制和扩展,或者你有其他原因不喜欢建议的解决方案,那么我担心你将不得不编写一个循环来写入输出一次一行,并使用低级文件写入函数。

问候

马克

dlmwrite writes a variable (for example, your array of M) to a file in ASCII text, as you know. Check the documentation for the function to discover how to set the separator character (to get , between values in each row) and how to append to an existing file.

To write the header line, I suggest you set up an array of strings, populate it and use dlmwrite to write it to your output file.

Now, if you want to write the rest of the file (the M array plus 2 leading columns of dates/times) you will need to create a temporary array with size the same as M plus 2 extra columns. Since your data values are floating-point numbers and your dates and times are structured data of some sort (strings perhaps) I think Mtemp will have to be an array of strings. Once you have created it you can append it to your output file with a single dlmwrite statement.

Note that num2str operates as you might hope on vectors but I'm not sure it can be coerced into putting , between values. It can, however, write numbers in a format you specify.

If this doesn't give you the output you want, or your array is too big to copy and expand or you have some other reason for not liking the proposed solution, then I fear that you will have to write a loop to write the output a line at a time, and use low-level file writing functions.

Regards

Mark

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文