改变 'fget1' while 循环以从单列文件中提取值以创建两列 .csv 文件,Matlab
Alter 'fget1' while loop to pull values from single column file to create two column .csv file, Matlab
我有大型 (1000,000+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 header 行和三个探针):
header1
header2
header3
0.0
4
3
2
0.1
7
6
5
0.2
9
8
7
我有大型 (1000,000+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 header 行和三个探针):
header1
header2
header3
0.0
4
3
2
0.1
7
6
5
0.2
9
8
7