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!