Writing a loop for processing the data in multiple text files in a folder

1 回表示 (過去 30 日間)
Luke Askew
Luke Askew 2021 年 11 月 25 日
回答済み: Shanmukha Voggu 2021 年 11 月 29 日
I'm attempting to process data from a folder with multiple text files, I want to take specific rows/columns from said text files and by applying the reshape/mean function I want to process each text file into a new set of data and write it into a new text file.
Files = dir('C:\Users\lukeaskew\Documents\MATLAB\SpineParametersData');
N = length(Files)
% loop for each file
for i = 1:N
thisfile = Files(i).txt;
AvLx = mean(reshape(thisfile(1:168, 2),28, []));
AvLy = mean(reshape(thisfile(1:168, 3), 28, []));
AvHx = mean(reshape(thisfile(179:192, 2), 14, []));
AvHy = mean(reshape(thisfile(179:192, 3), 14, []));
D = reshape(Avx,6,1);
E = reshape(Avy,6,1);
F = reshape(AvHx,1,1);
G = reshape(AvHy,1,1);
H = [D(:), E(:)];
I = [F(:), G(:)];
J = cat(1,H,I)
writematrix(J,'C:\Users\lukeaskew\Documents\MATLAB\SPDav\%d.txt', i)
end
The issue I am having is that when I print the value of N I get 0 and so the loop does not run through the folder, nor does it ultimately produce the datasets I require.
  1 件のコメント
Stephen23
Stephen23 2021 年 11 月 25 日
編集済み: Stephen23 2021 年 11 月 25 日
Probably you should specify a filename with widlcard characters, e.g.:
P = 'C:\Users\lukeaskew\Documents\MATLAB\SpineParametersData';
S = dir(fullfile(P,'*.txt'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
..
end

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

回答 (1 件)

Shanmukha Voggu
Shanmukha Voggu 2021 年 11 月 29 日
Hi Luke,
Adding to Stephen, In order to explore a similar type of example to get a better idea about looping through text files
please refer to this answer.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by