要使用textscan 读取文件,您必须在调用textscan 之前“打开”它并在之后“关闭”它;你应该使用
textscan 返回一个cellarray,其中包含从输入文件中读取的内容;由于您正在读取多个文件,因此您应该更改管理 textscan 返回的单元阵列的方式,实际上,就像现在在您的代码中一样,数据在每次迭代时都会被覆盖。
一种可能是将数据存储在struct 的数组中,例如,2 fields:输入文件的名称和数据。
另一种可能性是生成一个struct,每个字段都包含从输入文件中读取的数据;可以自动生成文件名。
另一种可能性是将它们存储到矩阵中。
之后,您可以找到一个脚本,其中已经实现了这三个替代方案。
代码更新(根据收到的评论)
为了能够正确读取95.04156.07等数据为95.04156.07,格式说明符应由%6f修改为%6.2f
% Get the list of input data
data=dir('input_file*.txt');
% Define the number of data column
n_data_col=12;
% Define the number of heared lines
n_header=2;
% Build the format specifier string
% OLD format specifier
formatspecific=[repmat('%6f',1,n_data_col) '%s']
% NEW format specifier
formatspecific=[repmat('%6.2f',1,n_data_col) '%s']
% Initialize the m_data matrix (if you know in advance the numer of row of
% each input file yoiu can define since the beginning the size of the
% matrix)
m_data=[];
% Loop for input file reading
for i=1:length(data)
% Get the i-th file name
file_name=data(i).name
% Open the i-th input file
fp=fopen(file_name,'rt')
% Read the i-th input file
C=textscan(fp,formatspecific,'headerlines',n_header)
% Close the input file
fclose(fp)
% Assign the read data to the "the_data" array struct
the_data(i).f_name=file_name
the_data(i).data=[C{1:end-1}]
% Assign the data to a struct whos fileds are named after the inout file
data_struct.(file_name(1:end-4))=[C{1:end-1}]
% Assign the data to the matric "m_data
m_data=[m_data;[C{1:end-1}]]
end
输入文件
TestData
6.84 11.31 17.51 22.62 26.91 31.98 36.47 35.85 28.47 20.57 10.50 6.37 test1
0.24 2.62 4.94 7.17 10.39 15.37 18.73 18.29 12.26 6.46 1.15 -0.33 test2
68.47 95.04156.07218.39304.31320.22311.69269.22203.01135.60 68.18 55.09 test3
68.47 95.04156.07218.39304.31320.22311.69269.22203.01135.60 68.18 55.09 test4
输出
m_data =
Columns 1 through 7
6.8400 11.3100 17.5100 22.6200 26.9100 31.9800 36.4700
0.2400 2.6200 4.9400 7.1700 10.3900 15.3700 18.7300
68.4700 95.0400 156.0700 218.3900 304.3100 320.2200 311.6900
68.4700 95.0400 156.0700 218.3900 304.3100 320.2200 311.6900
Columns 8 through 12
35.8500 28.4700 20.5700 10.5000 6.3700
18.2900 12.2600 6.4600 1.1500 -0.3300
269.2200 203.0100 135.6000 68.1800 55.0900
269.2200 203.0100 135.6000 68.1800 55.0900
希望这会有所帮助。