Reading data from .txt files from a folder.
3 views (last 30 days)
Show older comments
Pratheeba Chanda Nagarajan
on 10 Mar 2023
Commented: Pratheeba Chanda Nagarajan
on 10 Mar 2023
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0002-0.010000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0004-0.020000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0006-0.030000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0008-0.040000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0010-0.050000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0012-0.060000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0014-0.070000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0016-0.080000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0018-0.090000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0020-0.100000.txt
I am trying to read the files (.txt) from a folder and store them in a 3D Matrix. I have added the files here from the folder. ( I have added 10 files here, but I have 1500 files in the folder of the same size)
My matlab code:
% Reading Fluent solution files files from a folder
clc;
clear all;
close all;
% Define the folder path
folderPath = 'E:\Hayes_5Reactions_Troubleshooting\Transient_500K_Results\ExportData_CatalystInlet\';
% Get a list of all .txt files in the folder
fileList = dir(fullfile(folderPath, '*.txt'));
% Preallocate a 3D matrix to store the data
dataMatrix = zeros(46, 36, numel(fileList));
% Loop over each file and read the data
for i = 1:numel(fileList)
% Get the file name
fileName = fileList(i).name;
% Read the data from the file
fileData = dlmread(fullfile(folderPath, fileName),' ', 1,1);
% Store the data in the 3D matrix
dataMatrix(:,:,i) = fileData;
end
But when I see the output, for example: >> dataMatrix(:,:,1)
I see this:
Columns 1 through 22
0 0 0 0 0 0 0 0 1.0000 0 0.0710 0 0 0 6.3237 0 500.0968 0 0.0001 0 0.0666 0
0 0 0 0 0 0 0 0 2.0000 0 0.0710 0 0.0003 0 6.3320 0 500.0967 0 0.0001 0 0.0666 0
0 0 0 0 0 0 0 0 3.0000 0 0.0710 0 0.0006 0 6.3576 0 500.0967 0 0.0001 0 0.0666 0
0 0 0 0 0 0 0 0 4.0000 0 0.0710 0 0.0008 0 6.3997 0 500.0966 0 0.0001 0 0.0666 0
0 0 0 0 0 0 0 0 5.0000 0 0.0710 0 0.0011 0 6.4578 0 500.0965 0 0.0001 0 0.0666 0
0 0 0 0 0 0 0 0 6.0000 0 0.0710 0 0.0014 0 6.5328 0 500.0964 0 0.0001 0 0.0666 0
0 0 0 0 0 0 0 0 7.0000 0 0.0710 0 0.0017 0 6.6247 0 500.0963 0 0.0001 0 0.0666 0
0 0 0 0 0 0 0 0 8.0000 0 0.0710 0 0.0020 0 6.7332 0 500.0962 0 0.0001 0 0.0666 0
0 0 0 0 0 0 0 0 9.0000 0 0.0710 0 0.0023 0 6.8584 0 500.0961 0 0.0001 0 0.0666 0
0 0 0 0 0 0 0 10.0000 0 0.0710 0 0.0025 0 6.9986 0 500.0961 0 0.0001 0 0.0666 0 0.1526
0 0 0 0 0 0 0 11.0000 0 0.0710 0 0.0028 0 7.1413 0 500.0962 0 0.0001 0 0.0666 0 0.1526
0 0 0 0 0 0 0 12.0000 0 0.0710 0 0.0031 0 7.2519 0 500.0967 0 0.0001 0 0.0666 0 0.1526
0 0 0 0 0 0 0 13.0000 0 0.0710 0 0.0034 0 7.2874 0 500.0975 0 0.0001 0 0.0666 0 0.1526
0 0 0 0 0 0 0 14.0000 0 0.0710 0 0.0037 0 7.2142 0 500.0989 0 0.0001 0 0.0666 0 0.1526
0 0 0 0 0 0 0 15.0000 0 0.0710 0 0.0040 0 7.0177 0 500.1009 0 0.0001 0 0.0666 0 0.1526
0 0 0 0 0 0 0 16.0000 0 0.0710 0 0.0042 0 6.7132 0 500.1036 0 0.0001 0 0.0666 0 0.1526
0 0 0 0 0 0 0 17.0000 0 0.0710 0 0.0045 0 6.3369 0 500.1070 0 0.0001 0 0.0666 0 0.1526
0 0 0 0 0 0 0 18.0000 0 0.0710 0 0.0048 0 5.9283 0 500.1108 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 19.0000 0 0.0710 0 0.0051 0 5.5214 0 500.1148 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 20.0000 0 0.0710 0 0.0054 0 5.1377 0 500.1185 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 21.0000 0 0.0710 0 0.0056 0 4.7846 0 500.1213 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 22.0000 0 0.0710 0 0.0059 0 4.4556 0 500.1228 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 23.0000 0 0.0710 0 0.0062 0 4.1403 0 500.1228 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 24.0000 0 0.0710 0 0.0065 0 3.8341 0 500.1209 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 25.0000 0 0.0710 0 0.0068 0 3.5331 0 500.1169 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 26.0000 0 0.0710 0 0.0071 0 3.2403 0 500.1111 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 27.0000 0 0.0710 0 0.0073 0 2.9586 0 500.1041 0 0.0000 0 0.0665 0 0.1526
0 0 0 0 0 0 0 28.0000 0 0.0710 0 0.0076 0 2.6871 0 500.0965 0 0.0000 0 0.0665 0 0.1527
0 0 0 0 0 0 0 29.0000 0 0.0710 0 0.0079 0 2.4272 0 500.0895 0 0.0000 0 0.0665 0 0.1527
0 0 0 0 0 0 0 30.0000 0 0.0710 0 0.0082 0 2.1830 0 500.0839 0 0.0000 0 0.0665 0 0.1527
0 0 0 0 0 0 0 31.0000 0 0.0710 0 0.0085 0 1.9587 0 500.0805 0 0.0000 0 0.0664 0 0.1527
0 0 0 0 0 0 0 32.0000 0 0.0710 0 0.0087 0 1.7603 0 500.0796 0 0.0000 0 0.0664 0 0.1527
0 0 0 0 0 0 0 33.0000 0 0.0710 0 0.0090 0 1.5956 0 500.0808 0 0.0000 0 0.0664 0 0.1528
0 0 0 0 0 0 0 34.0000 0 0.0710 0 0.0093 0 1.4777 0 500.0837 0 0.0000 0 0.0664 0 0.1528
0 0 0 0 0 0 0 35.0000 0 0.0710 0 0.0096 0 1.4290 0 500.0877 0 0.0000 0 0.0664 0 0.1528
0 0 0 0 0 0 0 36.0000 0 0.0710 0 0.0099 0 1.4273 0 500.0924 0 0.0000 0 0.0664 0 0.1527
0 0 0 0 0 0 0 37.0000 0 0.0710 0 0.0102 0 1.4197 0 500.0972 0 0.0000 0 0.0664 0 0.1527
0 0 0 0 0 0 0 38.0000 0 0.0710 0 0.0104 0 1.3929 0 500.1019 0 0.0000 0 0.0665 0 0.1527
0 0 0 0 0 0 0 39.0000 0 0.0710 0 0.0107 0 1.3836 0 500.1063 0 0.0000 0 0.0665 0 0.1527
0 0 0 0 0 0 0 40.0000 0 0.0710 0 0.0110 0 1.4087 0 500.1105 0 0.0000 0 0.0665 0 0.1527
0 0 0 0 0 0 0 41.0000 0 0.0710 0 0.0113 0 1.4336 0 500.1142 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 42.0000 0 0.0710 0 0.0116 0 1.3955 0 500.1179 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 43.0000 0 0.0710 0 0.0119 0 1.3804 0 500.1216 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 44.0000 0 0.0710 0 0.0121 0 1.4635 0 500.1244 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 45.0000 0 0.0710 0 0.0124 0 1.3346 0 500.1241 0 0.0001 0 0.0665 0 0.1526
0 0 0 0 0 0 0 46.0000 0 0.0710 0 0.0127 0 0 0 500.1270 0 0.0001 0 0.0665 0 0.1526
Columns 23 through 36
0.1526 0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0
0.1526 0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0
0.1526 0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0
0.1526 0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0
0.1526 0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0
0.1526 0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0
0.1526 0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0
0.1526 0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0
0.1526 0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0625 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0625 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0625 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0625 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0625 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
0 0.0005 0 0.0002 0 0.0624 0 0.0000 0 0.0000 0 0.7177 0 0
The original text file has only 46 rows (excluding the title row) and 14 columns. Why do I get 36 columns? When I put 14 coulmns on line number 14, I get the columns is 36 , not able to preallocate with 14.
Can someone help understand what is the error here?
Thank you so much.
Pratheeba
3 Comments
Eric Delgado
on 10 Mar 2023
Edited: Eric Delgado
on 10 Mar 2023
dataMatrix = zeros(46, 14, numel(fileList));
And it seems there is an error using dlmread with zeros columns in the output. Just replace it for readtable. You can use the "Import Data" feature to obtain the optional arguments of readtable.
% Instead of:
fileData = dlmread("D:\HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0002-0.010000.txt", '', 1, 1)
% Try this:
fileData = readtable("D:\HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0002-0.010000.txt")
Accepted Answer
Stephen23
on 10 Mar 2023
Edited: Stephen23
on 10 Mar 2023
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0002-0.010000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0004-0.020000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0006-0.030000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0008-0.040000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0010-0.050000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0012-0.060000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0014-0.070000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0016-0.080000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0018-0.090000.txt
- HayesKineticsPaper2GeometryScaled_Transient_500K_cat_mid_0020-0.100000.txt
To make debugging unexpected data (sizes, types, values) easier, I usually find it easier to concatenate data after the loop:
P = '.'; % absolute or relative path to where the files are saved
S = dir(fullfile(P,'Hayes*.txt'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = readmatrix(F);
end
A = cat(3,S.data);
Checking the size and content:
size(A)
display(A)
3 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!