Process data from sequentially named folders each having a file with the same name

1 回表示 (過去 30 日間)
I have a set of sequentially named folders each containing a single file with the same name called event.txt that I wish to process (plot and save graphs as names of folders). Any ideas?
Here's what I have so far…
F = 'Event.txt';
J = ('C:\Users\myname\SM_Results\AnalysisCase.1');
S = dir(fullfile(J,'C1R*'));
X = [S.isdir] & ~ismember({S.name},{'.','..','AnalysisCase.1.C1R*'});
files = {S(X).name};
numfiles = length(X);
mydata = cell(1, numfiles);
for k = 1:numel(files)
existfilename = fullfile(J,files{k},F)
if exist(existfilename)
mydata{k} = importdata(existfilename);
%now process the data ...
data = readtable(existfilename);
time=data{:,'Time'};
Ty=data{:,'Expression_Point_E_Ty_value_mm_'};
[~,fileName,~] = fileparts(sprintf('Event_%d', k));% <-need help here to rename properly
figure
plot(time,Ty,'-')
xlabel('time, s')
ylabel('Ty (mm)')
title(fileName)
% savefig(fileName)
print(fileName,'-dpng')
  4 件のコメント
Stephen23
Stephen23 2019 年 4 月 10 日
編集済み: Stephen23 2019 年 4 月 10 日
Thank you for giving the subfolder names.
"where Event.txt does not exist theres three files AnalysisCase.1.C1R1.LMSMotionInfo, AnalysisCase.1.C1R1.LMSMotionResults andAnalysisCase.1.C1R1.LMSMotionSolverInput"
Sure. But your dir call does not look inside the subfolders where those files might be, it only looks in the main folder itself. And ismember does not have any "wildcard" characters.
"ok wil take another look."
files is misleading because they are names of folders, not files.
"wanted to rename Event.txt to Event_1.txt and name plots Event_1.png etc for each."
Just use sprintf to generate the required name directly.
Question: do you want the subfolders processed in numeric order, i.e. 1, 2, ...32, or is character order 1,10,...,2,20,,, 32, acceptable?
Stephen23
Stephen23 2019 年 4 月 10 日
david ocallaghan's "Answer" moved here:
"your dir call does not look inside the subfolders " yes of course thanks!
files as folders, yes I see, thanks.
ok will use sprintf, tried earlier but I didnt name it correctly (maybe because k wasnt matching numerical folder order)
I'd like the subfolder processed in numerical order - this will be a big fix allowing k to match C1R(k)
Thanks for your help, very insightful

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

採用された回答

Stephen23
Stephen23 2019 年 4 月 10 日
編集済み: Stephen23 2021 年 5 月 4 日
To get the subfolders in numeric order download my FEX submission natsortfiles:
And use it something like this:
F = 'Event.txt';
P = 'C:\Users\myname\SM_Results\AnalysisCase.1';
S = dir(fullfile(P,'C1R*'));
S = natsortfiles(S([S.isdir]));
for k = 1:numel(S)
Z = fullfile(P,S(k).name,F);
if 2==exist(Z,'file')
A = importdata(Z);
S(k).data = A;
... your code
G = sprintf('Event_%d.png',k);
... your code
print(G,...)
end
end
I doubt that you should make a new figure on every loop iteration.
Note that an alternative to natsortfiles would be to process the files in the order provided by your OS, extract the second number from the subfolder and use it as the index (instead of k).
Note that the behavior of dir changes if there is only one matching subfolder.

その他の回答 (1 件)

david ocallaghan
david ocallaghan 2019 年 4 月 11 日
Thanks you so much Stephen awesome!

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by