Чтобы использовать textscan
для чтения файла, вы должны "открыть" его перед вызовом textscan
и "закрыть" его после; вы должны использовать
fopen
, чтобы открыть входной файл
fclose
чтобы закрыть входной файл
textscan
возвращает cellarray
с содержимым, прочитанным из входного файла; поскольку вы читаете более одного файла, вам следует изменить способ управления массивом ячеек, возвращаемым textscan
, на самом деле, как сейчас в вашем коде, данные перезаписываются при каждой итерации.
Одной из возможностей может быть сохранение данных в массиве struct
, например, 2 fields
: имя входного файла и данные.
Другая возможность может состоять в том, чтобы сгенерировать struct
каждое поле которого содержит данные, считанные из входного файла; вы можете автоматически сгенерировать имя файла.
Другой возможностью может быть сохранение их в матрице.
Далее вы можете найти скрипт, в котором реализованы эти три альтернативы.
Код обновлен (после получения комментария)
Чтобы иметь возможность правильно читать данные, такие как 95.04156.07
как 95.04
156.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
Надеюсь это поможет.
person
il_raffa
schedule
19.10.2015