如何以与在MATLAB中读取的格式相同的格式编写文本文件? [英] How do I write a text file in the same format that it is read in MATLAB?

查看:138
本文介绍了如何以与在MATLAB中读取的格式相同的格式编写文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

然后处理文件并更改列的几个值。现在我想用新的文件 newfile.txt 以与我的 file.txt 格式相同的格式写入新的文件值。





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

它给了我一个新的文件,没有我的第一行标题,没有列1和列2我想要的。



我的文件格式如下:

  date时间,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.64
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.68
3.9 ,6.2, 6.4,6.2,0.64
4.6,5.5,6.1,5.8,0.5.6
7,7.2,6.9,0.6.3
code> pre>

有人能帮我把头文件和前2列复制到这个新文件吗?

解决方案

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

  fid = fopen('D:\file.txt , '室温'); %#打开文件
topLine = fgets(fid); %#阅读第一行并将其存储在字符串中
data = textscan(fid,'%f','Delimiter',',/:'); %#读取数据
fclose(fid); %#关闭文件

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

  N = 74; %#日期和时间之后的数据列数
data = reshape(data {1},N + 6,[])';

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

修改 data 中的值后,可以使用for循环和 FPRINTF 功能:

  fid = fopen('newfile1.txt','wt'); %#打开文件
fprintf(fid,'%s',topLine); %#打印最上面一行
for i = 1:size(data,1)%#循环遍历数据行
fprintf(fid,'%d /%d /%d,%d: %d:%d,数据(i,1:6)); %#打印日期
fprintf(fid,',%.1f',data(i,7:end)); %#打印数据
fprintf(fid,'\\\
'); %#打印一个换行符
结束
fclose(fid); %#关闭文件

我用 86400-by-80 矩阵为 data ,大约需要30秒才能将数据写入文件。


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?

解决方案

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.

这篇关于如何以与在MATLAB中读取的格式相同的格式编写文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆