How can I import multiple .out files in a single folder as separate tables or arrays?
조회 수: 1 (최근 30일)
이전 댓글 표시

I want to import these multi-column files into MATLAB. I tried the code attached, however the following error prompts.
Error using matlab.io.ImportOptions/readtable (line 503)
Unable to find or open 'xw2-rfile.out'. Check the path and filename or file permissions.
Error in mat6mm (line 46)
vxw{i} = readtable(filename,opts);
xw = [2,5,8,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,40,60,80,100];
vxw = zeros(size(xw));
%vxw2-rfile%
for i = 1:1:size(xw)
xwstr = string(xw(i));
filename = strcat('xw',xwstr,'-rfile.out');
opts = delimitedTextImportOptions("NumVariables", 3);
opts.DataLines = [4, Inf];
opts.Delimiter = " ";
opts.VariableNames = ["Time Step", "Velocity (m/s)", "Flow Time (s)"];
opts.VariableTypes = ["double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
vxw{i} = readtable(filename,opts);
clear opts
end
댓글 수: 2
채택된 답변
Stephen23
2025년 3월 24일
unzip reports
P = '.'; % absolute or relative path to where the files are saved
S = dir(fullfile(P,'vxw*.out'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
T = readtable(F, 'FileType','delimitedtext', 'NumHeaderLines',3);
T.Properties.VariableNames = ["Time Step", "Velocity (m/s)", "Flow Time (s)"];
S(k).data = T;
end
All imported file data are stored in the structure S, together with the filenames:
S.data
댓글 수: 3
추가 답변 (1개)
dpb
2025년 3월 24일
편집: dpb
2025년 3월 24일
unzip reports.zip
d=dir('vx*.*');
for i = 1:numel(d)
filename=fullfile(d(i).folder,d(i).name);
vxw{i} = readtable(filename,'FileType','text');
vxw{i}.Properties.VariableNames={'Time', 'Velocity','FlowTime'};
vxw{i}.Properties.VariableUnits={'s', 'm/s','s'};
end
head(vxw{1})
summary(vxw{1})
Using a suitable wildcard for desired files makes the code more generic and removes the need to bury the "magic" constants in the code and to manually create filenames--only the matching root filename wildcard string is then needed besides the variable identification data.
I shortened variable names to not have embedded blanks so can use "dot" referencing of the table names without quoting for coding convenience and place the units in the variables metadata reserved for them...
The ".out" filename extension is not one of those recognized by MATLAB automagically as a text file, you have to tell the functions that specifically.
These files don't need an import object, but If you really, really wanted to use one, it could go outside the loop since all the files are the same format...and all can set is the variable names; the units metadata field is not supported..
opt=detectImportOptions(fullfile(d(1).folder,d(1).name),'FileType','Text');
opt.VariableNames={'Time', 'Velocity','FlowTime'}
for i = 1:numel(d)
filename=fullfile(d(i).folder,d(i).name);
vxw{i} = readtable(filename,opt);
vxw{i}.Properties.VariableUnits={'s', 'm/s','s'};
end
summary(vxw{i})
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!