Read multiple text files as separate matrices

2 ビュー (過去 30 日間)
fadzhi
fadzhi 2021 年 2 月 1 日
コメント済み: Rik 2021 年 2 月 1 日
I have multiple text files and i want to import each of them as separate data (the number of text files is changing) I have taken some help from the previously asked question and written a small code but i am not being able to import any data. I have attached the text file and the code. I will really appreciate any help i this regard.
input_folder = 'T:New\files';
files = dir(fullfile(input_folder, '*.txt'));
file_paths = fullfile({files.folder}, {files.name});
for i = 1 : numel(file_paths)
format = '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f';
data = textscan(file_paths{i}, format,'headerlines', 7);
end

採用された回答

Rik
Rik 2021 年 2 月 1 日
The textscan function requires either an fid as input, or a character array. Your code doesn't actually read the data from the file, but from the file name.
It will also overwrite the variable data every file.
  2 件のコメント
fadzhi
fadzhi 2021 年 2 月 1 日
編集済み: fadzhi 2021 年 2 月 1 日
thanks.....working
clear all;
D = 'T:\New\files';
S = dir(fullfile(D, '*.txt'));
N = length(S);
A = cell(N,1);
fmt = '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f';
% fmt=repmat('%f',1,2);
for k = 1:N
fid = fopen(fullfile(D,S(k).name), 'r');
A(k)= textscan(fid, fmt, 'Delimiter', '\t', ...
'headerlines', 7, ...
'CollectOutput', 1);
fid=fclose(fid);
end
Rik
Rik 2021 年 2 月 1 日
Glad to be of help.
You should avoid clear all, and use clear or clearvars instead.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by