更改 'fget1' while 循环以从单列文件中提取值以创建两列.csv文件、Matlab



我有一个大的(1000000 KB以上(单列.txt文件,其中包含以特定时间间隔来自52个探测器的数据(格式见下文(。通过阅读其他文章,我有一个程序可以将单个探针的数据放入一个文件中(见下面的程序(。

是否有人建议如何更改while循环,以包含与探测数据值逗号分隔的相应时间值(请参阅下面所需的格式(?我希望能够给同事们一个.txt文件,他们可以导入Excel来绘制探测数据。

***how my single column data .txt file is formatted***
59 lines of header information
time 1
probe 1 data
probe 2 data
...50 other probes
time 2     
probe 1 data
probe 2 data
...50 other probes
and so on

% program to put probe 1 data (which repeats every 51 lines) into a file
m = 1;
d = fopen('C:path.txt');
while ~feof(d)
    for n=1:59
      tline = fgetl(d);
    end
  while ischar(tline)
      tline = fgetl(d);
      Probe1(m).txt = tline;
     m = m+1;
     for n=1:51
      tline = fgetl(d);
     end
  end
end
fclose(d);asdf

***desired format for single probe data .txt file***
time 1, probe 1 data at time 1
time 2, probe 1 data at time 2
and so on...

感谢您的时间和任何帮助。

检查以下代码。您需要添加以读取时间值并存储它们。然后您可以使用dlmwrite来保存csv-文件。

clc; clear;
num_head = 3;   % number of header lines
num_probes = 3; % number of probes
f = fopen('data.txt','r');
% Ignore header
for i = 1:num_head
    fgetl(f);
end
t_num = 1; % Actual time step
while ~feof(f)
    time(t_num,1) = str2double(fgetl(f));
    for p_num = 1:num_probes % loop through probes
        probe_data(t_num,p_num) = str2double(fgetl(f));
    end
    t_num = t_num+1;
end
fclose(f);
for p_num = 1:num_probes
    dlmwrite(sprintf('data_probes_%d.csv',p_num),[time probe_data(:,p_num)],',');
end

示例数据文件(每个时间点有3个标题行和3个探针(:

header1
header2
header3
0.0
4
3
2
0.1
7
6
5
0.2
9
8
7

最新更新