- what are the names of folders?
- why parentheses around the J character vector?
- what is 'AnalysisCase.1.C1R*' supposed to match? (the function ismember interprets all characters literally, there are no wildcards).
- files is a very misleading name.
- why do you import the data twice?
- what is fileparts supposed to do?
- please align your code consistently and format it properly on this page using the code formatting buttons.
Process data from sequentially named folders each having a file with the same name
1 view (last 30 days)
Show older comments
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 Comments
Stephen23
on 10 Apr 2019
Edited: Stephen23
on 10 Apr 2019
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
on 10 Apr 2019
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
Accepted Answer
Stephen23
on 10 Apr 2019
Edited: Stephen23
on 4 May 2021
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.
0 Comments
More Answers (1)
See Also
Categories
Find more on File Operations 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!