Attempting to automate the importing of text file data

3 ビュー (過去 30 日間)
Jacob Lobao
Jacob Lobao 2019 年 3 月 22 日
コメント済み: Jacob Lobao 2019 年 3 月 22 日
I am trying to automate the importing of text file data. How can I call each individual text file name, which is variable, in the readtable function? I have a variable with each textfile name, but am having difficulty using that in the readtable function. Is that a good way to do it? Im not even sure. I attached two text files for examples, any help is appreciated.
textfiles = dir('*.txt');
numboffiles = length(textfiles);
filelist = {ones(1:numboffiles)};
for j =1:numboffiles
filelist(j) = {textfiles(j).name};
end
data = ones(length(filelist));
for i = 1:length(filelist)
fid = fopen(filelist{i});
if fid == -1
fprintf ('Wrong File Name');
else
data(i) = readtable(filelist(j), 'Delimiter', '\t', 'ReadVariableNames',false, 'HeaderLines', 9);
end
end
  2 件のコメント
KSSV
KSSV 2019 年 3 月 22 日
YOu can read your file with readtable.......
but am having difficulty using that in the readtable function what difficulty?
You already asked multiple questions int he same context of text file...but you have neevr discussed or acknowledges your previous questions. You should discuss at your previous questions..if you are happy with the answer provided ..accept the answer then you can ask a different question.
Jacob Lobao
Jacob Lobao 2019 年 3 月 22 日
I understand, thanks for letting me know. I am able to use readtable for the text files, but when im running the code above, I get an error, which I believe to be from when I am attempting to call each text file with filelist(j) at the bottom of the code, rather than specifying by exact name, a single file.

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 3 月 22 日
%Let directory 'data' have your files: 40_8deg_HL_Both.txt and 40_4deg_HL_Both.txt
n = dir('path_to_data/data/*Both.txt');
m = numel(n);
T = cell(m,1);
vn = {'Data_Timestamp','q','V_ref','Alpha','NFonSF','AFonAF2','PMonYM','P','Orientation'};
for ii = 1:m
T{ii} = readtable(n(1).name,'ReadVariableNames',0,...
'Format','%{yyyyMMdd HH:mm:ss.SSS}D %f %f %f %f %f %f %f %q',...
'HeaderLines',9);
T{ii}.Properties.VariableNames = vn;
end
%or other for-loop
for ii = 1:m
T{ii} = readtable(n(1).name,'ReadVariableNames',0,...
'Format','%q %q %f %f %f %f %f %f %f %q','HeaderLines',9);
T{ii}.Var1 = datetime(strcat(T{ii}{:,1},'_',T{ii}{:,2}),'I','uuuuMMdd_HH:mm:ss.SSS');
T{ii}.Var2 = [];
T{ii}.Properties.VariableNames = vn;
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by